Skip to content

Commit

Permalink
feat: add flag to skip compression, remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
skipi committed Nov 14, 2023
1 parent 183f228 commit 8c33620
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 251 deletions.
15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ run:
go run main.go $(arg)

regen:
go run main.go compile priv/parsers/generic/in.xml priv/parsers/generic/out.json
go run main.go compile priv/parsers/rspec/in.xml priv/parsers/rspec/out.json
go run main.go compile priv/parsers/exunit/in.xml priv/parsers/exunit/out.json
go run main.go compile priv/parsers/golang/in.xml priv/parsers/golang/out.json
go run main.go compile -p phpunit priv/parsers/phpunit/in.xml priv/parsers/phpunit/out.json
go run main.go compile -p embedded priv/parsers/embedded/in.xml priv/parsers/embedded/out.json
go run main.go compile priv/merging priv/merging/out.json
go run main.go compile --no-compress priv/parsers/generic/in.xml priv/parsers/generic/out.json
go run main.go compile --no-compress priv/parsers/rspec/in.xml priv/parsers/rspec/out.json
go run main.go compile --no-compress priv/parsers/exunit/in.xml priv/parsers/exunit/out.json
go run main.go compile --no-compress priv/parsers/golang/in.xml priv/parsers/golang/out.json
go run main.go compile --no-compress -p phpunit priv/parsers/phpunit/in.xml priv/parsers/phpunit/out.json
go run main.go compile --no-compress -p embedded priv/parsers/embedded/in.xml priv/parsers/embedded/out.json
go run main.go compile --no-compress priv/merging priv/merging/out.json

test:
gotestsum ./...

Expand Down
7 changes: 6 additions & 1 deletion cmd/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ var combineCmd = &cobra.Command{
return err
}

skipCompression, err := cmd.Flags().GetBool("no-compress")
if err != nil {
return err
}

paths, err := cli.LoadFiles(inputs, ".json")
if err != nil {
return err
Expand Down Expand Up @@ -71,7 +76,7 @@ var combineCmd = &cobra.Command{
return err
}

_, err = cli.WriteToFilePath(jsonData, output)
_, err = cli.WriteToFilePath(jsonData, output, !skipCompression)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ var compileCmd = &cobra.Command{
output := args[len(args)-1]

err := cli.SetLogLevel(cmd)
if err != nil {
return err
}

skipCompression, err := cmd.Flags().GetBool("no-compress")
if err != nil {
return err
}
Expand Down Expand Up @@ -79,7 +83,7 @@ var compileCmd = &cobra.Command{
return err
}

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

_, err = cli.WriteToFilePath(jsonData, output)
_, err = cli.WriteToFilePath(jsonData, output, !skipCompression)
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/gen-pipeline-report.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var genPipelineReportCmd = &cobra.Command{
if err != nil {
return err
}
skipCompression, err := cmd.Flags().GetBool("no-compress")
if err != nil {
return err
}

var dir string

Expand Down Expand Up @@ -78,7 +82,7 @@ var genPipelineReportCmd = &cobra.Command{
return err
}

fileName, err := cli.WriteToTmpFile(jsonData)
fileName, err := cli.WriteToTmpFile(jsonData, !skipCompression)
if err != nil {
return err
}
Expand All @@ -94,6 +98,10 @@ var genPipelineReportCmd = &cobra.Command{
}

func pushSummaries(testResult []parser.TestResults, level, path string, cmd *cobra.Command) error {
skipCompression, err := cmd.Flags().GetBool("no-compress")
if err != nil {
return err
}
if len(testResult) == 0 {
logger.Info("no test results to process")
return nil
Expand All @@ -111,7 +119,7 @@ func pushSummaries(testResult []parser.TestResults, level, path string, cmd *cob
return err
}

summaryFileName, err := cli.WriteToTmpFile(jsonSummary)
summaryFileName, err := cli.WriteToTmpFile(jsonSummary, !skipCompression)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ var publishCmd = &cobra.Command{
return err
}

skipCompression, err := cmd.Flags().GetBool("no-compress")
if err != nil {
return err
}

paths, err := cli.LoadFiles(inputs, ".xml")
if err != nil {
return err
Expand Down Expand Up @@ -78,7 +83,7 @@ var publishCmd = &cobra.Command{
return err
}

_, err = cli.WriteToFile(jsonData, tmpFile)
_, err = cli.WriteToFile(jsonData, tmpFile, !skipCompression)
if err != nil {
return err
}
Expand All @@ -100,7 +105,7 @@ var publishCmd = &cobra.Command{
return err
}

fileName, err := cli.WriteToTmpFile(jsonData)
fileName, err := cli.WriteToTmpFile(jsonData, !skipCompression)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
rootCmd.PersistentFlags().StringP("name", "N", "", "name of the suite")
rootCmd.PersistentFlags().StringP("suite-prefix", "S", "", "prefix for each suite")
rootCmd.PersistentFlags().StringP("parser", "p", "auto", "override parser to be used")
rootCmd.PersistentFlags().Bool("no-compress", false, "skip gzip compression for the output")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down
150 changes: 0 additions & 150 deletions cmd/test-results-collector.go

This file was deleted.

28 changes: 16 additions & 12 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,44 +195,48 @@ func Marshal(testResults parser.Result) ([]byte, error) {
return jsonData, nil
}

func WriteToFile(data []byte, file *os.File) (string, error) {
return writeToFile(data, file)
func WriteToFile(data []byte, file *os.File, compress bool) (string, error) {
return writeToFile(data, file, compress)
}

// WriteToFilePath saves data to given file
func WriteToFilePath(data []byte, path string) (string, error) {
func WriteToFilePath(data []byte, path string, compress bool) (string, error) {
file, err := os.Create(filepath.Clean(path))
if err != nil {
logger.Error("Opening file %s: %v", path, err)
return "", err
}
defer file.Close()

return writeToFile(data, file)
return writeToFile(data, file, compress)
}

// WriteToTmpFile saves data to temporary file
func WriteToTmpFile(data []byte) (string, error) {
func WriteToTmpFile(data []byte, compress bool) (string, error) {
file, err := os.CreateTemp("", "test-results")
if err != nil {
logger.Error("Opening file %s: %v", file.Name(), err)
return "", err
}
defer file.Close()

return writeToFile(data, file)
return writeToFile(data, file, compress)
}

func writeToFile(data []byte, file *os.File) (string, error) {
func writeToFile(data []byte, file *os.File, compress bool) (string, error) {
logger.Info("Saving results to %s", file.Name())

compressedData, err := GzipCompress(data)
if err != nil {
logger.Error("Output file write failed: %v", err)
return "", err
dataToWrite := data
if compress {
compressedData, err := GzipCompress(data)
if err != nil {
logger.Error("Output file write failed: %v", err)
return "", err
}
dataToWrite = compressedData
}

_, err = file.Write(compressedData)
_, err := file.Write(dataToWrite)
if err != nil {
logger.Error("Output file write failed: %v", err)
return "", err
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestWriteToTmpFile(t *testing.T) {
jsonData, _ := json.Marshal(&result)

t.Run("Write to one tmp file", func(t *testing.T) {
file, err := cli.WriteToTmpFile(jsonData)
file, err := cli.WriteToTmpFile(jsonData, false)
assert.NoError(t, err)
os.Remove(file)
})
Expand All @@ -133,7 +133,7 @@ func TestWriteToTmpFile(t *testing.T) {
for i := 0; i < fileNumber; i++ {
go func() {
defer wg.Done()
file, err := cli.WriteToTmpFile(jsonData)
file, err := cli.WriteToTmpFile(jsonData, false)
defer os.Remove(file)
assert.NoError(t, err)

Expand Down Expand Up @@ -168,7 +168,7 @@ func TestWriteToFilePath(t *testing.T) {
jsonData, _ := json.Marshal(&result)

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

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

0 comments on commit 8c33620

Please sign in to comment.