1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-11 17:50:58 +00:00

refactor: remove gotest.tools (#688)

* refactor: remove `gotest.tools`

* remove all references to `gotest.tools` and replace it with
  `github.com/stretchr/testify` which was originally used for tests
* bump `golangci-lint` version
* add `depguard` and `importas` to prevent import of unwanted packages
* add custom schema and information about config since
  schemastore.org has broken schema for `golangci-lint` config

* fix: handle more error cases
This commit is contained in:
Ryan (hackercat) 2021-05-18 06:14:49 +00:00 committed by GitHub
parent ee4a5c458f
commit e784133c16
7 changed files with 50 additions and 42 deletions

View file

@ -16,7 +16,7 @@ jobs:
env: env:
CGO_ENABLED: 0 CGO_ENABLED: 0
with: with:
version: v1.39.0 version: v1.40.0
- uses: github/super-linter@v3 - uses: github/super-linter@v3
env: env:
DEFAULT_BRANCH: master DEFAULT_BRANCH: master

View file

@ -5,7 +5,7 @@ import (
"testing" "testing"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gotest.tools/v3/assert" assert "github.com/stretchr/testify/assert"
) )
func init() { func init() {
@ -33,7 +33,7 @@ func TestGetImagePullOptions(t *testing.T) {
ctx := context.Background() ctx := context.Background()
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{}) options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
assert.NilError(t, err, "Failed to create ImagePullOptions") assert.Nil(t, err, "Failed to create ImagePullOptions")
assert.Equal(t, options.RegistryAuth, "", "RegistryAuth should be empty if no username or password is set") assert.Equal(t, options.RegistryAuth, "", "RegistryAuth should be empty if no username or password is set")
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{ options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
@ -41,6 +41,6 @@ func TestGetImagePullOptions(t *testing.T) {
Username: "username", Username: "username",
Password: "password", Password: "password",
}) })
assert.NilError(t, err, "Failed to create ImagePullOptions") assert.Nil(t, err, "Failed to create ImagePullOptions")
assert.Equal(t, options.RegistryAuth, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", "Username and Password should be provided") assert.Equal(t, options.RegistryAuth, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", "Username and Password should be provided")
} }

View file

@ -8,7 +8,7 @@ import (
"testing" "testing"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
a "github.com/stretchr/testify/assert" assert "github.com/stretchr/testify/assert"
) )
func TestEvaluate(t *testing.T) { func TestEvaluate(t *testing.T) {
@ -115,14 +115,14 @@ func TestEvaluate(t *testing.T) {
for _, table := range tables { for _, table := range tables {
table := table table := table
t.Run(table.in, func(t *testing.T) { t.Run(table.in, func(t *testing.T) {
assert := a.New(t) assertObject := assert.New(t)
out, _, err := ee.Evaluate(table.in) out, _, err := ee.Evaluate(table.in)
if table.errMesg == "" { if table.errMesg == "" {
assert.NoError(err, table.in) assertObject.NoError(err, table.in)
assert.Equal(table.out, out, table.in) assertObject.Equal(table.out, out, table.in)
} else { } else {
assert.Error(err, table.in) assertObject.Error(err, table.in)
assert.Equal(table.errMesg, err.Error(), table.in) assertObject.Equal(table.errMesg, err.Error(), table.in)
} }
}) })
} }
@ -188,9 +188,9 @@ func TestInterpolate(t *testing.T) {
for _, table := range tables { for _, table := range tables {
table := table table := table
t.Run(table.in, func(t *testing.T) { t.Run(table.in, func(t *testing.T) {
assert := a.New(t) assertObject := assert.New(t)
out := ee.Interpolate(table.in) out := ee.Interpolate(table.in)
assert.Equal(table.out, out, table.in) assertObject.Equal(table.out, out, table.in)
}) })
} }
} }
@ -277,9 +277,9 @@ func TestRewrite(t *testing.T) {
for _, table := range tables { for _, table := range tables {
table := table table := table
t.Run(table.in, func(t *testing.T) { t.Run(table.in, func(t *testing.T) {
assert := a.New(t) assertObject := assert.New(t)
re := ee.Rewrite(table.in) re := ee.Rewrite(table.in)
assert.Equal(table.re, re, table.in) assertObject.Equal(table.re, re, table.in)
}) })
} }
} }

View file

@ -10,11 +10,10 @@ import (
"testing" "testing"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
a "github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test" "github.com/sirupsen/logrus/hooks/test"
assert "github.com/stretchr/testify/assert"
) )
func TestRunContext_EvalBool(t *testing.T) { func TestRunContext_EvalBool(t *testing.T) {
@ -142,15 +141,15 @@ func TestRunContext_EvalBool(t *testing.T) {
for _, table := range tables { for _, table := range tables {
table := table table := table
t.Run(table.in, func(t *testing.T) { t.Run(table.in, func(t *testing.T) {
assert := a.New(t) assertObject := assert.New(t)
defer hook.Reset() defer hook.Reset()
b, err := rc.EvalBool(table.in) b, err := rc.EvalBool(table.in)
if table.wantErr { if table.wantErr {
assert.Error(err) assertObject.Error(err)
} }
assert.Equal(table.out, b, fmt.Sprintf("Expected %s to be %v, was %v", table.in, table.out, b)) assertObject.Equal(table.out, b, fmt.Sprintf("Expected %s to be %v, was %v", table.in, table.out, b))
assert.Empty(hook.LastEntry(), table.in) assertObject.Empty(hook.LastEntry(), table.in)
}) })
} }
} }
@ -269,10 +268,10 @@ func TestRunContext_GetBindsAndMounts(t *testing.T) {
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
fullBind += ":delegated" fullBind += ":delegated"
} }
a.Contains(t, gotbind, fullBind) assert.Contains(t, gotbind, fullBind)
} else { } else {
mountkey := testcase.rc.jobContainerName() mountkey := testcase.rc.jobContainerName()
a.EqualValues(t, testcase.wantmount, gotmount[mountkey]) assert.EqualValues(t, testcase.wantmount, gotmount[mountkey])
} }
}) })
} }
@ -284,7 +283,7 @@ func TestGetGitHubContext(t *testing.T) {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
cwd, err := os.Getwd() cwd, err := os.Getwd()
assert.NilError(t, err) assert.Nil(t, err)
rc := &RunContext{ rc := &RunContext{
Config: &Config{ Config: &Config{

View file

@ -11,17 +11,17 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gotest.tools/v3/assert" assert "github.com/stretchr/testify/assert"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
) )
func TestGraphEvent(t *testing.T) { func TestGraphEvent(t *testing.T) {
planner, err := model.NewWorkflowPlanner("testdata/basic", true) planner, err := model.NewWorkflowPlanner("testdata/basic", true)
assert.NilError(t, err) assert.Nil(t, err)
plan := planner.PlanEvent("push") plan := planner.PlanEvent("push")
assert.NilError(t, err) assert.Nil(t, err)
assert.Equal(t, len(plan.Stages), 3, "stages") assert.Equal(t, len(plan.Stages), 3, "stages")
assert.Equal(t, len(plan.Stages[0].Runs), 1, "stage0.runs") assert.Equal(t, len(plan.Stages[0].Runs), 1, "stage0.runs")
assert.Equal(t, len(plan.Stages[1].Runs), 1, "stage1.runs") assert.Equal(t, len(plan.Stages[1].Runs), 1, "stage1.runs")
@ -46,7 +46,7 @@ type TestJobFileInfo struct {
func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) { func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) {
t.Run(tjfi.workflowPath, func(t *testing.T) { t.Run(tjfi.workflowPath, func(t *testing.T) {
workdir, err := filepath.Abs(tjfi.workdir) workdir, err := filepath.Abs(tjfi.workdir)
assert.NilError(t, err, workdir) assert.Nil(t, err, workdir)
fullWorkflowPath := filepath.Join(workdir, tjfi.workflowPath) fullWorkflowPath := filepath.Join(workdir, tjfi.workflowPath)
runnerConfig := &Config{ runnerConfig := &Config{
Workdir: workdir, Workdir: workdir,
@ -59,18 +59,18 @@ func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) {
} }
runner, err := New(runnerConfig) runner, err := New(runnerConfig)
assert.NilError(t, err, tjfi.workflowPath) assert.Nil(t, err, tjfi.workflowPath)
planner, err := model.NewWorkflowPlanner(fullWorkflowPath, true) planner, err := model.NewWorkflowPlanner(fullWorkflowPath, true)
assert.NilError(t, err, fullWorkflowPath) assert.Nil(t, err, fullWorkflowPath)
plan := planner.PlanEvent(tjfi.eventName) plan := planner.PlanEvent(tjfi.eventName)
err = runner.NewPlanExecutor(plan)(ctx) err = runner.NewPlanExecutor(plan)(ctx)
if tjfi.errorMessage == "" { if tjfi.errorMessage == "" {
assert.NilError(t, err, fullWorkflowPath) assert.Nil(t, err, fullWorkflowPath)
} else { } else {
assert.ErrorContains(t, err, tjfi.errorMessage) assert.Error(t, err, tjfi.errorMessage)
} }
}) })
} }
@ -138,7 +138,7 @@ func TestRunEventSecrets(t *testing.T) {
eventName := "push" eventName := "push"
workdir, err := filepath.Abs("testdata") workdir, err := filepath.Abs("testdata")
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
env, _ := godotenv.Read(filepath.Join(workdir, workflowPath, ".env")) env, _ := godotenv.Read(filepath.Join(workdir, workflowPath, ".env"))
secrets, _ := godotenv.Read(filepath.Join(workdir, workflowPath, ".secrets")) secrets, _ := godotenv.Read(filepath.Join(workdir, workflowPath, ".secrets"))
@ -152,15 +152,15 @@ func TestRunEventSecrets(t *testing.T) {
Env: env, Env: env,
} }
runner, err := New(runnerConfig) runner, err := New(runnerConfig)
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
planner, err := model.NewWorkflowPlanner(fmt.Sprintf("testdata/%s", workflowPath), true) planner, err := model.NewWorkflowPlanner(fmt.Sprintf("testdata/%s", workflowPath), true)
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
plan := planner.PlanEvent(eventName) plan := planner.PlanEvent(eventName)
err = runner.NewPlanExecutor(plan)(ctx) err = runner.NewPlanExecutor(plan)(ctx)
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
} }
func TestRunEventPullRequest(t *testing.T) { func TestRunEventPullRequest(t *testing.T) {
@ -179,7 +179,7 @@ func TestRunEventPullRequest(t *testing.T) {
eventName := "pull_request" eventName := "pull_request"
workdir, err := filepath.Abs("testdata") workdir, err := filepath.Abs("testdata")
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
runnerConfig := &Config{ runnerConfig := &Config{
Workdir: workdir, Workdir: workdir,
@ -189,15 +189,15 @@ func TestRunEventPullRequest(t *testing.T) {
ReuseContainers: false, ReuseContainers: false,
} }
runner, err := New(runnerConfig) runner, err := New(runnerConfig)
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
planner, err := model.NewWorkflowPlanner(fmt.Sprintf("testdata/%s", workflowPath), true) planner, err := model.NewWorkflowPlanner(fmt.Sprintf("testdata/%s", workflowPath), true)
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
plan := planner.PlanEvent(eventName) plan := planner.PlanEvent(eventName)
err = runner.NewPlanExecutor(plan)(ctx) err = runner.NewPlanExecutor(plan)(ctx)
assert.NilError(t, err, workflowPath) assert.Nil(t, err, workflowPath)
} }
func TestContainerPath(t *testing.T) { func TestContainerPath(t *testing.T) {

View file

@ -89,11 +89,19 @@ func (sc *StepContext) Executor() common.Executor {
Dir: actionDir, Dir: actionDir,
Token: github.Token, Token: github.Token,
}) })
var ntErr common.Executor
if err := gitClone(context.TODO()); err != nil { if err := gitClone(context.TODO()); err != nil {
if err.Error() == "short SHA references are not supported" {
err = errors.Cause(err) err = errors.Cause(err)
return common.NewErrorExecutor(fmt.Errorf("Unable to resolve action `%s`, the provided ref `%s` is the shortened version of a commit SHA, which is not supported. Please use the full commit SHA `%s` instead", step.Uses, remoteAction.Ref, err.Error())) return common.NewErrorExecutor(fmt.Errorf("Unable to resolve action `%s`, the provided ref `%s` is the shortened version of a commit SHA, which is not supported. Please use the full commit SHA `%s` instead", step.Uses, remoteAction.Ref, err.Error()))
} else if err.Error() != "some refs were not updated" {
return common.NewErrorExecutor(err)
} else {
ntErr = common.NewInfoExecutor("Non-terminating error while running 'git clone': %v", err)
}
} }
return common.NewPipelineExecutor( return common.NewPipelineExecutor(
ntErr,
sc.setupAction(actionDir, remoteAction.Path), sc.setupAction(actionDir, remoteAction.Path),
sc.runAction(actionDir, remoteAction.Path), sc.runAction(actionDir, remoteAction.Path),
) )

View file

@ -1,8 +1,9 @@
package cmd package cmd
import ( import (
"log"
"path/filepath" "path/filepath"
log "github.com/sirupsen/logrus"
) )
// Input contains the input for the root command // Input contains the input for the root command