Skip to content

Commit

Permalink
Close temporary file after writing parsed test results (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamir-suspect authored Aug 25, 2023
1 parent eb4fd3f commit e484b8c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var combineCmd = &cobra.Command{
return err
}

_, err = cli.WriteToFile(jsonData, output)
_, err = cli.WriteToFilePath(jsonData, output)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var compileCmd = &cobra.Command{
return err
}

_, err = cli.WriteToFile(jsonData, tmpFile.Name())
_, err = cli.WriteToFilePath(jsonData, tmpFile.Name())
if err != nil {
return err
}
Expand All @@ -96,7 +96,7 @@ var compileCmd = &cobra.Command{
return err
}

_, err = cli.WriteToFile(jsonData, output)
_, err = cli.WriteToFilePath(jsonData, output)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ var publishCmd = &cobra.Command{
return err
}

_, err = cli.WriteToFile(jsonData, tmpFile.Name())
_, err = cli.WriteToFile(jsonData, tmpFile)
if err != nil {
return err
}

err = tmpFile.Close()
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ func Marshal(testResults parser.Result) ([]byte, error) {
return jsonData, nil
}

// WriteToFile saves data to given file
func WriteToFile(data []byte, path string) (string, error) {
func WriteToFile(data []byte, file *os.File) (string, error) {
return writeToFile(data, file)
}

// WriteToFilePathPath saves data to given file
func WriteToFilePath(data []byte, path string) (string, error) {
file, err := os.Create(path) // #nosec
defer file.Close()

Expand Down
11 changes: 6 additions & 5 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cli_test
import (
"encoding/json"
"fmt"
"github.com/semaphoreci/test-results/pkg/parser"
"github.com/stretchr/testify/require"
"os"
"testing"

"github.com/semaphoreci/test-results/pkg/parser"
"github.com/stretchr/testify/require"

"github.com/semaphoreci/test-results/pkg/cli"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestWriteToTmpFile(t *testing.T) {
})
}

func TestWriteToFile(t *testing.T) {
func TestWriteToFilePath(t *testing.T) {
tr := parser.TestResults{
ID: "1234",
Name: "Test",
Expand All @@ -159,7 +160,7 @@ func TestWriteToFile(t *testing.T) {
jsonData, _ := json.Marshal(&result)

t.Run("Write to one file", func(t *testing.T) {
file, err := cli.WriteToFile(jsonData, "out")
file, err := cli.WriteToFilePath(jsonData, "out")
assert.NoError(t, err)
os.Remove(file)
})
Expand All @@ -175,7 +176,7 @@ func TestWriteToFile(t *testing.T) {
tmpFile, err := os.CreateTemp(dirPath, "result-*.json")
require.NoError(t, err)

_, err = cli.WriteToFile(jsonData, tmpFile.Name())
_, err = cli.WriteToFilePath(jsonData, tmpFile.Name())
require.NoError(t, err)
}
})
Expand Down

0 comments on commit e484b8c

Please sign in to comment.