2023-01-08 03:53:03 +01:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testMetadata() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"CI": "woodpecker",
|
|
|
|
// Repository
|
|
|
|
"CI_REPO": "woodpecker-ci/woodpecker",
|
|
|
|
"CI_REPO_NAME": "woodpecker",
|
|
|
|
"CI_REPO_OWNER": "woodpecker-ci",
|
|
|
|
"CI_REPO_SCM": "git",
|
2023-12-07 11:45:09 +01:00
|
|
|
"CI_REPO_URL": "https://codeberg.org/woodpecker-plugins/go-plugin",
|
|
|
|
"CI_REPO_CLONE_URL": "https://codeberg.org/woodpecker-plugins/go-plugin.git",
|
2023-01-08 03:53:03 +01:00
|
|
|
"CI_REPO_DEFAULT_BRANCH": "main",
|
|
|
|
"CI_REPO_PRIVATE": "false",
|
|
|
|
// Commit
|
|
|
|
"CI_COMMIT_SHA": "a1b2c3d4",
|
|
|
|
"CI_COMMIT_REF": "refs/heads/main",
|
|
|
|
"CI_COMMIT_REFSPEC": "refs/heads/main",
|
|
|
|
"CI_COMMIT_BRANCH": "main",
|
|
|
|
"CI_COMMIT_MESSAGE": "test commit",
|
|
|
|
"CI_COMMIT_AUTHOR": "John Doe",
|
|
|
|
"CI_COMMIT_AUTHOR_EMAIL": "john@example.com",
|
|
|
|
"CI_COMMIT_AUTHOR_AVATAR": "https://avatars.githubusercontent.com/u/1234567?v=4",
|
2023-12-07 11:45:09 +01:00
|
|
|
"CI_COMMIT_URL": "https://codeberg.org/woodpecker-plugins/go-plugin/commit/a1b2c3d4",
|
2023-01-08 03:53:03 +01:00
|
|
|
// Build
|
|
|
|
"CI_PIPELINE_NUMBER": "1",
|
2023-12-10 21:27:57 +01:00
|
|
|
"CI_PIPELINE_EVENT": EventTypePush,
|
2023-12-07 11:45:09 +01:00
|
|
|
"CI_PIPELINE_URL": "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1",
|
2023-01-08 03:53:03 +01:00
|
|
|
"CI_PIPELINE_STATUS": "running",
|
|
|
|
"CI_PIPELINE_CREATED": "1611234567",
|
|
|
|
"CI_PIPELINE_STARTED": "1611234567",
|
2024-11-13 02:01:15 +01:00
|
|
|
"CI_PIPELINE_FILES": `["README.md", "main.go"]`,
|
2023-01-08 03:53:03 +01:00
|
|
|
// Step
|
|
|
|
"CI_STEP_NUMBER": "1",
|
|
|
|
"CI_STEP_NAME": "test",
|
|
|
|
"CI_STEP_STATUS": "running",
|
|
|
|
"CI_STEP_STARTED": "1611234567",
|
|
|
|
// System
|
|
|
|
"CI_SYSTEM_NAME": "woodpecker",
|
2023-12-07 11:45:09 +01:00
|
|
|
"CI_SYSTEM_URL": "https://ci.woodpecker-ci.org",
|
2023-01-08 03:53:03 +01:00
|
|
|
"CI_SYSTEM_VERSION": "1.0.0",
|
|
|
|
"CI_SYSTEM_HOST": "woodpecker-ci.org",
|
2023-12-10 21:27:57 +01:00
|
|
|
// Forge
|
|
|
|
"CI_FORGE_TYPE": ForgeTypeGitea,
|
|
|
|
"CI_FORGE_URL": "https://codeberg.org",
|
2023-01-08 03:53:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadata(t *testing.T) {
|
|
|
|
for k, v := range testMetadata() {
|
|
|
|
os.Setenv(k, v)
|
|
|
|
defer os.Unsetenv(k)
|
|
|
|
}
|
|
|
|
plugin := New(Options{
|
|
|
|
Execute: func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2024-07-23 01:32:14 +02:00
|
|
|
err := plugin.app.Run(plugin.ctx, []string{"test"})
|
2023-01-08 03:53:03 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Repository
|
|
|
|
assert.Equal(t, "woodpecker", plugin.Metadata.Repository.Name)
|
|
|
|
assert.Equal(t, "woodpecker-ci", plugin.Metadata.Repository.Owner)
|
2024-02-15 19:53:26 +01:00
|
|
|
assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin", plugin.Metadata.Repository.URL)
|
2023-12-07 11:45:09 +01:00
|
|
|
assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin.git", plugin.Metadata.Repository.CloneURL)
|
2023-01-08 03:53:03 +01:00
|
|
|
assert.Equal(t, "main", plugin.Metadata.Repository.Branch)
|
|
|
|
assert.False(t, plugin.Metadata.Repository.Private)
|
|
|
|
|
|
|
|
// Commit
|
|
|
|
assert.Equal(t, "a1b2c3d4", plugin.Metadata.Curr.Sha)
|
|
|
|
assert.Equal(t, "refs/heads/main", plugin.Metadata.Curr.Ref)
|
|
|
|
assert.Equal(t, "refs/heads/main", plugin.Metadata.Curr.Refspec)
|
|
|
|
assert.Equal(t, "main", plugin.Metadata.Curr.Branch)
|
|
|
|
assert.Equal(t, "test commit", plugin.Metadata.Curr.Message)
|
|
|
|
assert.Equal(t, "John Doe", plugin.Metadata.Curr.Author.Name)
|
|
|
|
assert.Equal(t, "john@example.com", plugin.Metadata.Curr.Author.Email)
|
|
|
|
assert.Equal(t, "https://avatars.githubusercontent.com/u/1234567?v=4", plugin.Metadata.Curr.Author.Avatar)
|
|
|
|
|
|
|
|
// Pipeline
|
|
|
|
assert.Equal(t, int64(1), plugin.Metadata.Pipeline.Number)
|
2023-12-10 21:27:57 +01:00
|
|
|
assert.Equal(t, EventTypePush, plugin.Metadata.Pipeline.Event)
|
2024-02-15 19:53:26 +01:00
|
|
|
assert.Equal(t, "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", plugin.Metadata.Pipeline.URL)
|
2023-01-08 03:53:03 +01:00
|
|
|
assert.Equal(t, "running", plugin.Metadata.Pipeline.Status)
|
|
|
|
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Created)
|
|
|
|
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Started)
|
2024-11-13 02:01:15 +01:00
|
|
|
assert.Equal(t, []string{"README.md", "main.go"}, plugin.Metadata.Pipeline.ChangedFiles)
|
2023-01-08 03:53:03 +01:00
|
|
|
|
|
|
|
// Step
|
|
|
|
assert.Equal(t, 1, plugin.Metadata.Step.Number)
|
|
|
|
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Step.Started)
|
|
|
|
|
|
|
|
// System
|
|
|
|
assert.Equal(t, "woodpecker", plugin.Metadata.System.Name)
|
2024-02-15 19:53:26 +01:00
|
|
|
assert.Equal(t, "https://ci.woodpecker-ci.org", plugin.Metadata.System.URL)
|
2023-01-08 03:53:03 +01:00
|
|
|
assert.Equal(t, "1.0.0", plugin.Metadata.System.Version)
|
|
|
|
assert.Equal(t, "woodpecker-ci.org", plugin.Metadata.System.Host)
|
2023-12-10 21:27:57 +01:00
|
|
|
|
|
|
|
// Forge
|
|
|
|
assert.Equal(t, ForgeTypeGitea, plugin.Metadata.Forge.Type)
|
2024-02-15 19:53:26 +01:00
|
|
|
assert.Equal(t, "https://codeberg.org", plugin.Metadata.Forge.URL)
|
2023-01-08 03:53:03 +01:00
|
|
|
}
|