Skip to content

Commit

Permalink
Merge pull request #6 from s0ders/ci/pinned-version
Browse files Browse the repository at this point in the history
ci/pinned version
  • Loading branch information
s0ders authored Apr 30, 2024
2 parents a0d317a + c71790a commit ea80b84
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
82 changes: 82 additions & 0 deletions cmd/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ea80b84

Please sign in to comment.