-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
175 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
changed: | ||
- Errors are outputted to stderr instead of stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package conflictless_test | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/ypjama/conflictless-keepachangelog/internal/pkg/conflictless" | ||
) | ||
|
||
//nolint:paralleltest // this test is not parallel because it modifies os.Stdout. | ||
func TestCheck(t *testing.T) { | ||
cfg := new(conflictless.Config) | ||
|
||
changesDir, err := os.MkdirTemp(os.TempDir(), "changes") | ||
assert.NoError(t, err) | ||
|
||
defer os.RemoveAll(changesDir) | ||
|
||
changesFile := createFile(t, changesDir, "test-check.json") | ||
defer os.Remove(changesFile.Name()) | ||
|
||
stdOutFile := createTempFile(t, os.TempDir(), "stdout-test-check") | ||
defer os.Remove(stdOutFile.Name()) | ||
|
||
writeDataToFile(t, []byte(`{"fixed":["foo"]}`), changesFile) | ||
|
||
cfg.Flags.Directory = &changesDir | ||
|
||
os.Stdout = stdOutFile | ||
|
||
conflictless.Check(cfg) | ||
|
||
data, err := os.ReadFile(stdOutFile.Name()) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "Change files are valid!\n", string(data)) | ||
} | ||
|
||
func TestCheckWhenDirectoryDoesNotExist(t *testing.T) { | ||
t.Parallel() | ||
|
||
if os.Getenv("TEST_CHECK_WHEN_ERROR") == "1" { | ||
cfg := new(conflictless.Config) | ||
|
||
nonExistentDir := os.TempDir() + "/foo/bar" | ||
cfg.Flags.Directory = &nonExistentDir | ||
|
||
conflictless.Check(cfg) | ||
|
||
return | ||
} | ||
|
||
//nolint:gosec // this is a test package so G204 doesn't really matter here. | ||
cmd := exec.Command(os.Args[0], "-test.run=^TestCheckWhenDirectoryDoesNotExist$") | ||
|
||
cmd.Env = append(os.Environ(), "TEST_CHECK_WHEN_ERROR=1") | ||
err := cmd.Run() | ||
|
||
exitErr := new(*exec.ExitError) | ||
errors.As(err, exitErr) | ||
|
||
expectedCode := 2 | ||
exitCode := (*exitErr).ExitCode() | ||
|
||
assert.Equal(t, expectedCode, exitCode, "process exited with %d, want exit status %d", expectedCode, exitCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package conflictless_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
const ( | ||
mkdirFileMode = 0o755 | ||
writeFileMode = 0o644 | ||
) | ||
|
||
func writeDataToFile(t *testing.T, data []byte, file *os.File) { | ||
t.Helper() | ||
|
||
err := os.WriteFile(file.Name(), data, writeFileMode) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func createFile(t *testing.T, dir, name string) *os.File { | ||
t.Helper() | ||
|
||
file, err := os.Create(dir + "/" + name) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return file | ||
} | ||
|
||
func createTempFile(t *testing.T, dir, pattern string) *os.File { | ||
t.Helper() | ||
|
||
file, err := os.CreateTemp(dir, pattern) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return file | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters