Skip to content

Commit

Permalink
more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
preslavgerchev authored and chris-rock committed Dec 20, 2023
1 parent 5caf5c4 commit c45c418
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
6 changes: 4 additions & 2 deletions cli/reporter/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package reporter
import (
"context"
"os"
"strings"

"github.com/rs/zerolog/log"
"go.mondoo.com/cnspec/v9/policy"
Expand All @@ -19,7 +20,8 @@ type localFileHandler struct {
// we reuse the already implemented Reporter's WriteReport method by simply pointing the writer
// towards a file instead of stdout
func (h *localFileHandler) WriteReport(ctx context.Context, report *policy.ReportCollection) error {
f, err := os.Create(h.file)
trimmedFile := strings.TrimPrefix(h.file, "file://")
f, err := os.Create(trimmedFile)
if err != nil {
return err
}
Expand All @@ -33,6 +35,6 @@ func (h *localFileHandler) WriteReport(ctx context.Context, report *policy.Repor
if err != nil {
return err
}
log.Info().Str("file", h.file).Msg("wrote report to file")
log.Info().Str("file", trimmedFile).Msg("wrote report to file")
return nil
}
56 changes: 40 additions & 16 deletions cli/reporter/file_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"

Expand All @@ -25,21 +26,44 @@ func TestFileHandler(t *testing.T) {
require.NoError(t, err)

now := time.Now().Format(time.RFC3339)
fileName := fmt.Sprintf("/tmp/%s-testfilehandler.json", now)
config := HandlerConfig{Format: "compact", OutputTarget: fileName}
handler, err := NewOutputHandler(config)
require.NoError(t, err)
err = handler.WriteReport(context.Background(), yr)
require.NoError(t, err)
data, err := os.ReadFile(fileName)
require.NoError(t, err)
t.Run("with no prefix", func(t *testing.T) {
fileName := fmt.Sprintf("/tmp/%s-testfilehandler.json", now)
config := HandlerConfig{Format: "compact", OutputTarget: fileName}
handler, err := NewOutputHandler(config)
require.NoError(t, err)
err = handler.WriteReport(context.Background(), yr)
require.NoError(t, err)
data, err := os.ReadFile(fileName)
require.NoError(t, err)

strData := string(data)
assert.Contains(t, strData, "✕ Fail: Ensure")
assert.Contains(t, strData, ". Skipped: Set")
assert.Contains(t, strData, "! Error: Set")
assert.Contains(t, strData, "✓ Pass: A 100 Ensure")
assert.Contains(t, strData, "✕ Fail: F 0 Ensure")
err = os.Remove(fileName)
require.NoError(t, err)
strData := string(data)
assert.Contains(t, strData, "✕ Fail: Ensure")
assert.Contains(t, strData, ". Skipped: Set")
assert.Contains(t, strData, "! Error: Set")
assert.Contains(t, strData, "✓ Pass: A 100 Ensure")
assert.Contains(t, strData, "✕ Fail: F 0 Ensure")
err = os.Remove(fileName)
require.NoError(t, err)
})

t.Run("with file:// prefix", func(t *testing.T) {
fileName := fmt.Sprintf("file:///tmp/%s-testfilehandler.json", now)
config := HandlerConfig{Format: "compact", OutputTarget: fileName}
handler, err := NewOutputHandler(config)
require.NoError(t, err)
err = handler.WriteReport(context.Background(), yr)
require.NoError(t, err)
trimmed := strings.TrimPrefix(fileName, "file://")
data, err := os.ReadFile(trimmed)
require.NoError(t, err)

strData := string(data)
assert.Contains(t, strData, "✕ Fail: Ensure")
assert.Contains(t, strData, ". Skipped: Set")
assert.Contains(t, strData, "! Error: Set")
assert.Contains(t, strData, "✓ Pass: A 100 Ensure")
assert.Contains(t, strData, "✕ Fail: F 0 Ensure")
err = os.Remove(trimmed)
require.NoError(t, err)
})
}

0 comments on commit c45c418

Please sign in to comment.