From 21025017a92db401c2857eae718304107f16f161 Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Thu, 9 Jun 2022 10:16:10 +0200 Subject: [PATCH 1/2] ci: add unit test task to the CI --- .gitlab-ci.yml | 21 +++++++++++++++++---- commands/ci/trace/trace_test.go | 6 ++++-- commands/cmdutils/cmdutils_test.go | 4 ++-- pkg/git/git_test.go | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4b86fc6d6..75e5f4e7f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,19 +1,19 @@ workflow: rules: # For merge requests, create a pipeline. - - if: '$CI_MERGE_REQUEST_IID' + - if: "$CI_MERGE_REQUEST_IID" # For default branch, create a pipeline (this includes on schedules, pushes, merges, etc.). # - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' # Temporarly create Ci for every branch: - - if: '$CI_COMMIT_BRANCH' + - if: "$CI_COMMIT_BRANCH" # For tags, create a pipeline. - - if: '$CI_COMMIT_TAG' + - if: "$CI_COMMIT_TAG" # For stable branches, create a pipeline. - if: '$CI_COMMIT_BRANCH =~ /^[\d-]+-stable$/' # default: - image: golang:1.15 + image: golang:1.18 variables: SAST_EXCLUDED_ANALYZERS: "bandit,gosec,eslint" @@ -31,3 +31,16 @@ code_navigation: artifacts: reports: lsif: dump.lsif + +run_tests: + before_script: + ## Adding private SSH key to the executor, more information: https://docs.gitlab.com/ee/ci/ssh_keys/#ssh-keys-when-using-the-docker-executor + - mkdir -p ~/.ssh + - chmod 700 ~/.ssh + - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts + - chmod 600 ~/.ssh/known_hosts + - eval $(ssh-agent -s) + - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - + + script: + - make test diff --git a/commands/ci/trace/trace_test.go b/commands/ci/trace/trace_test.go index 73d98846e..5975cead7 100644 --- a/commands/ci/trace/trace_test.go +++ b/commands/ci/trace/trace_test.go @@ -46,13 +46,15 @@ hosts: repo := cmdtest.CopyTestRepo(t, "ci_trace_test") gitCmd := exec.Command("git", "fetch", "origin") gitCmd.Dir = repo - if _, err := gitCmd.CombinedOutput(); err != nil { + if out, err := gitCmd.CombinedOutput(); err != nil { + t.Error(string(out)) t.Fatal(err) } gitCmd = exec.Command("git", "checkout", "test-cli") gitCmd.Dir = repo - if _, err := gitCmd.CombinedOutput(); err != nil { + if out, err := gitCmd.CombinedOutput(); err != nil { + t.Error(string(out)) t.Fatal(err) } diff --git a/commands/cmdutils/cmdutils_test.go b/commands/cmdutils/cmdutils_test.go index 590932ea7..f63744272 100644 --- a/commands/cmdutils/cmdutils_test.go +++ b/commands/cmdutils/cmdutils_test.go @@ -259,7 +259,7 @@ func Test_UserAssignmentsAPIFailure(t *testing.T) { t.Errorf("UsersFromReplaces() expected error to not be nil") } if want != err.Error() { - t.Errorf("UsersFromReplace() expected error = %s, got = %w", want, err) + t.Errorf("UsersFromReplace() expected error = %s, got = %v", want, err) } _, _, err = ua.UsersFromAddRemove(nil, nil, &apiClient, nil) @@ -267,7 +267,7 @@ func Test_UserAssignmentsAPIFailure(t *testing.T) { t.Errorf("UsersFromReplaces() expected error to not be nil") } if want != err.Error() { - t.Errorf("UsersFromReplace() expected error = %s, got = %w", want, err) + t.Errorf("UsersFromReplace() expected error = %s, got = %v", want, err) } } diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 9d57a806f..60b6919d4 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -291,7 +291,7 @@ func TestGetRemoteURL(t *testing.T) { { name: "isInvalid", remoteAlias: "origin", - want: "profclems/glab", + want: "gitlab-org/cli", }, } for _, tt := range tests { -- GitLab From 343f2252030e2c7c21a53351821ad3cd08fe469e Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Fri, 10 Jun 2022 14:20:44 +0200 Subject: [PATCH 2/2] test: support forks CI in automated tests --- pkg/git/git_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 60b6919d4..a42594fc8 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -1,6 +1,7 @@ package git import ( + "os" "os/exec" "reflect" "testing" @@ -13,6 +14,13 @@ import ( "github.com/stretchr/testify/require" ) +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} + func Test_isFilesystemPath(t *testing.T) { type args struct { p string @@ -291,7 +299,7 @@ func TestGetRemoteURL(t *testing.T) { { name: "isInvalid", remoteAlias: "origin", - want: "gitlab-org/cli", + want: getEnv("CI_PROJECT_PATH","gitlab-org/cli"), }, } for _, tt := range tests { -- GitLab