Skip to content

Commit

Permalink
Add more tests, reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed Nov 22, 2024
1 parent ab210af commit 0bba7ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {

debug.Log("Mapping environment variables...", dOptions.Debug)
MapEnvToOptions(&dOptions, e)

if dOptions.GenerateCommitName && dOptions.CommitName == "" {
debug.Log("No commit name set, generating commit name", dOptions.Debug)
dOptions.CommitName = GenerateCommitNameTimestamp()
}
UpdatedEmptyCommitName(&dOptions)

if err := SetWorkingDirectory(&dOptions); err != nil {
return err
Expand Down Expand Up @@ -339,6 +335,13 @@ func SetWorkingDirectory(d *DebrickedOptions) error {
return nil
}

func UpdatedEmptyCommitName(o *DebrickedOptions) {
if o.GenerateCommitName && o.CommitName == "" {
debug.Log("No commit name set, generating commit name", o.Debug)
o.CommitName = GenerateCommitNameTimestamp()
}
}

func GenerateCommitNameTimestamp() string {
return fmt.Sprintf("generated-%d", time.Now().Unix())
}
Expand Down
29 changes: 29 additions & 0 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,35 @@ var cases = []struct {
},
}

func TestUpdateEmptyCommitName(t *testing.T) {
opts := DebrickedOptions{
CommitName: "",
GenerateCommitName: true,
}

UpdatedEmptyCommitName(&opts)
assert.Contains(t, opts.CommitName, "generated-")
}

func TestUpdateEmptyCommitNameNotEmpty(t *testing.T) {
opts := DebrickedOptions{
CommitName: "test",
GenerateCommitName: true,
}

UpdatedEmptyCommitName(&opts)
assert.Equal(t, opts.CommitName, "test")
}

func TestUpdateEmptyCommitNameNoGenerateCommitNameSet(t *testing.T) {
opts := DebrickedOptions{
CommitName: "",
}

UpdatedEmptyCommitName(&opts)
assert.Empty(t, opts.CommitName)
}

func TestGenerateCommitName(t *testing.T) {
res := GenerateCommitNameTimestamp()
assert.Contains(t, res, "generated-")
Expand Down

0 comments on commit 0bba7ed

Please sign in to comment.