Skip to content

Commit

Permalink
Add additional test coverage for helper functions in Util
Browse files Browse the repository at this point in the history
Currently helper functions in Util lacks test coverage

This commit fixes the following functions by addressing the review feedback.
- TestOpenFileArg: create testfile.txt outside the test case code, to avoid needing as much conditional logic in the test.

Signed-off-by: Kugamoorthy Gajananan <[email protected]>
  • Loading branch information
gajananan committed Jan 27, 2025
1 parent 6c53d65 commit 7a03106
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions internal/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,13 @@ owner: repoOwner
func TestOpenFileArg(t *testing.T) {
t.Parallel()

testDir := t.TempDir()
tempFilePath := filepath.Join(testDir, "testfile.txt")
err := os.WriteFile(tempFilePath, []byte("test content"), 0600)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}

testCases := []struct {
name string
filePath string
Expand All @@ -615,7 +622,7 @@ func TestOpenFileArg(t *testing.T) {
},
{
name: "Valid file path",
filePath: "testfile.txt",
filePath: tempFilePath,
dashOpen: nil,
expectedDesc: "test content",
expectError: false,
Expand All @@ -630,23 +637,9 @@ func TestOpenFileArg(t *testing.T) {
}

for _, tc := range testCases {
tc := tc
testDir := t.TempDir()
var tempFilePath string

t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// Create a temporary file for testing
if tc.filePath == "testfile.txt" {
tempFilePath = filepath.Join(testDir, tc.filePath)
err := os.WriteFile(tempFilePath, []byte(tc.expectedDesc), 0600)
assert.NoError(t, err)
defer os.Remove(tempFilePath)
} else {
// When handling the dash (-) as a file path, it should read from the provided io.Reader (tc.dashOpen) instead of trying to open a file
tempFilePath = tc.filePath
}
desc, closer, err := util.OpenFileArg(tempFilePath, tc.dashOpen)
desc, closer, err := util.OpenFileArg(tc.filePath, tc.dashOpen)
if closer != nil {
defer closer()
}
Expand All @@ -660,7 +653,6 @@ func TestOpenFileArg(t *testing.T) {
_, err := io.Copy(buf, desc)
assert.NoError(t, err)
assert.Equal(t, tc.expectedDesc, buf.String())

}
})
}
Expand Down

0 comments on commit 7a03106

Please sign in to comment.