From 290f5ec2b4b0b1133109c79bdad8f6e8b01198ec Mon Sep 17 00:00:00 2001 From: s0ders <39492740+s0ders@users.noreply.github.com> Date: Tue, 30 Apr 2024 18:25:05 +0200 Subject: [PATCH 1/2] test: increased test coverage --- cmd/local_test.go | 82 ++++++++++++++++++++++++++++++++++ internal/parser/parser_test.go | 23 ++++++++++ 2 files changed, 105 insertions(+) diff --git a/cmd/local_test.go b/cmd/local_test.go index 0222ff4..e8d9c11 100644 --- a/cmd/local_test.go +++ b/cmd/local_test.go @@ -194,6 +194,88 @@ func TestLocalCmd_Verbose(t *testing.T) { assert.NoError(err, "local command executed with error") } +func TestLocalCmd_CustomRules(t *testing.T) { + assert := assert.New(t) + + // Setting up sample Git repository + repository, repositoryPath, err := sampleRepository() + assert.NoError(err, "failed to create sample repository") + + defer func() { + err = os.RemoveAll(repositoryPath) + assert.NoError(err, "failed to remove repository") + }() + + commitTypes := []string{ + "fix", // 0.1.0 (with custom rules) + "feat", // 0.2.0 + } + + for _, commitType := range commitTypes { + err = sampleCommit(repository, repositoryPath, commitType) + assert.NoError(err, "failed to create sample commit") + } + + tempRulesDir, err := os.MkdirTemp("", "rules-*") + assert.NoError(err, "failed to create temp. dir.") + + defer func() { + err = os.RemoveAll(tempRulesDir) + assert.NoError(err, "failed to remove temp. dir.") + }() + + customRulesPath := filepath.Join(tempRulesDir, "custom.json") + + customRules, err := os.Create(customRulesPath) + assert.NoError(err, "failed to create empty rule file") + + customRulesJSON := ` +{ + "rules": [ + {"type": "feat", "release": "minor"}, + {"type": "fix", "release": "minor"} + ] +} +` + + _, err = customRules.Write([]byte(customRulesJSON)) + assert.NoError(err, "failed to write empty rule file") + + defer func() { + err = customRules.Close() + assert.NoError(err, "failed to close empty rule file") + }() + + actual := new(bytes.Buffer) + rootCmd.SetOut(actual) + rootCmd.SetErr(actual) + rootCmd.SetArgs([]string{"local", repositoryPath, "--tag-prefix", "v", "--rules-path", customRulesPath, "--release-branch", "main", "--json"}) + + err = rootCmd.Execute() + assert.NoError(err, "local command executed with error") + + expectedVersion := "0.2.0" + expectedTag := "v" + expectedVersion + expectedOut := cmdOutput{ + Message: "new release found", + NewVersion: expectedVersion, + NewRelease: true, + } + actualOut := cmdOutput{} + + err = json.Unmarshal(actual.Bytes(), &actualOut) + assert.NoError(err, "failed to unmarshal json") + + // Check that the JSON output is correct + assert.Equal(expectedOut, actualOut, "localCmd output should be equal") + + // Check that the tag was actually created on the repository + exists, err := tag.TagExists(repository, expectedTag) + assert.NoError(err, "failed to check if tag exists") + + assert.Equal(true, exists, "tag should exist") +} + func sampleRepository() (*git.Repository, string, error) { dir, err := os.MkdirTemp("", "localcmd-test-*") if err != nil { diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index a1af4b0..d07bfab 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -211,6 +211,29 @@ func TestParser_ComputeNewSemverNumberWithUntaggedRepositoryWitPatchRelease(t *t assert.Equal(want, version.String(), "version should be equal") } +func TestParser_UnknownReleaseType(t *testing.T) { + assert := assert.New(t) + + r, repositoryPath, err := createGitRepository("fix: commit that trigger an unknown release") + assert.NoError(err, "should have been able to create git repository") + + defer func(path string) { + err := os.RemoveAll(repositoryPath) + assert.NoError(err, "should have able to remove git repository") + }(repositoryPath) + + rules := &rules.ReleaseRules{ + Rules: []rules.ReleaseRule{ + {CommitType: "fix", ReleaseType: "unknown"}, + }, + } + + ca := New(fakeLogger, rules) + + _, _, err = ca.ComputeNewSemver(r) + assert.Error(err, "should have been failed trying to compute semver") +} + func TestParser_ComputeNewSemverNumberWithUntaggedRepositoryWitMinorRelease(t *testing.T) { assert := assert.New(t) From c71790a0399f9822027bd8c6eb0cfd2a14273461 Mon Sep 17 00:00:00 2001 From: s0ders <39492740+s0ders@users.noreply.github.com> Date: Tue, 30 Apr 2024 18:26:02 +0200 Subject: [PATCH 2/2] ci: fixed job env. var. --- .github/workflows/main.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index d1076de..0912cc2 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -51,6 +51,7 @@ jobs: if: ${{needs.go-build.outputs.release == 'true'}} env: PRERELEASE_TAG: '${{needs.go-build.outputs.semver}}-${{ github.sha }}' + RELEASE_TAG: ${{needs.go-build.outputs.semver}} steps: - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4