go-plugin/metadata_test.go
6543 67b6cdf4a6 Migrate to github.com/urfave/cli/v3 (#29)
Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/29
Reviewed-by: Patrick Schratz <pat-s@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
2024-07-22 23:32:14 +00:00

125 lines
4.7 KiB
Go

// 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",
"CI_REPO_URL": "https://codeberg.org/woodpecker-plugins/go-plugin",
"CI_REPO_CLONE_URL": "https://codeberg.org/woodpecker-plugins/go-plugin.git",
"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",
"CI_COMMIT_URL": "https://codeberg.org/woodpecker-plugins/go-plugin/commit/a1b2c3d4",
// Build
"CI_PIPELINE_NUMBER": "1",
"CI_PIPELINE_EVENT": EventTypePush,
"CI_PIPELINE_URL": "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1",
"CI_PIPELINE_STATUS": "running",
"CI_PIPELINE_CREATED": "1611234567",
"CI_PIPELINE_STARTED": "1611234567",
// Step
"CI_STEP_NUMBER": "1",
"CI_STEP_NAME": "test",
"CI_STEP_STATUS": "running",
"CI_STEP_STARTED": "1611234567",
// System
"CI_SYSTEM_NAME": "woodpecker",
"CI_SYSTEM_URL": "https://ci.woodpecker-ci.org",
"CI_SYSTEM_VERSION": "1.0.0",
"CI_SYSTEM_HOST": "woodpecker-ci.org",
// Forge
"CI_FORGE_TYPE": ForgeTypeGitea,
"CI_FORGE_URL": "https://codeberg.org",
}
}
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
},
})
err := plugin.app.Run(plugin.ctx, []string{"test"})
require.NoError(t, err)
// Repository
assert.Equal(t, "woodpecker", plugin.Metadata.Repository.Name)
assert.Equal(t, "woodpecker-ci", plugin.Metadata.Repository.Owner)
assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin", plugin.Metadata.Repository.URL)
assert.Equal(t, "https://codeberg.org/woodpecker-plugins/go-plugin.git", plugin.Metadata.Repository.CloneURL)
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)
assert.Equal(t, EventTypePush, plugin.Metadata.Pipeline.Event)
assert.Equal(t, "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", plugin.Metadata.Pipeline.URL)
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)
// 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)
assert.Equal(t, "https://ci.woodpecker-ci.org", plugin.Metadata.System.URL)
assert.Equal(t, "1.0.0", plugin.Metadata.System.Version)
assert.Equal(t, "woodpecker-ci.org", plugin.Metadata.System.Host)
// Forge
assert.Equal(t, ForgeTypeGitea, plugin.Metadata.Forge.Type)
assert.Equal(t, "https://codeberg.org", plugin.Metadata.Forge.URL)
}