8000 feat: implement --error by cedws · Pull Request #237 · stacklok/frizbee · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: implement --error #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func replaceCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

if err := cliFlags.CheckModified(res.Modified); err != nil {
return err
}

// Process the output files
return cliFlags.ProcessOutput(dir, res.Processed, res.Modified)
}
Expand Down
16 changes: 16 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cli

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -165,6 +166,21 @@ func (r *Helper) Logf(format string, args ...interface{}) {
}
}

// CheckModified checks if any files were modified and returns an error if there were.
func (r *Helper) CheckModified(modified map[string]string) error {
if len(modified) > 0 && r.ErrOnModified {
if !r.Quiet {
for path := range modified {
r.Logf("Modified: %s\n", path)
}
}

return errors.New("files were modified")
}

return nil
}

// ProcessOutput processes the given output files.
// If the command is quiet, the output is discarded.
// If the command is a dry run, the output is written to the command's stdout.
Expand Down
96 changes: 96 additions & 0 deletions internal/cli/cli_test.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,102 @@ func TestNewHelper(t *testing.T) {
}
}

func TestCheckModified(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
helper *Helper
modified map[string]string
expectOutput string
expectError bool
}{
{
name: "NoFilesModified",
helper: &Helper{
ErrOnModified: true,
Quiet: false,
Cmd: &cobra.Command{},
},
modified: map[string]string{},
expectOutput: "",
expectError: false,
},
{
name: "FilesModifiedWithErrorFlag",
helper: &Helper{
ErrOnModified: true,
Quiet: false,
Cmd: &cobra.Command{},
},
modified: map[string]string{
"file1.txt": "modified content",
"file2.txt": "another modified content",
},
expectOutput: "Modified: file1.txt\nModified: file2.txt",
expectError: true,
},
{
name: "FilesModifiedWithoutErrorFlag",
helper: &Helper{
ErrOnModified: false,
Quiet: false,
Cmd: &cobra.Command{},
},
modified: map[string]string{
"file1.txt": "modified content",
},
expectOutput: "",
expectError: false,
},
{
name: "FilesModifiedWithErrorFlagAndQuiet",
helper: &Helper{
ErrOnModified: true,
Quiet: true,
Cmd: &cobra.Command{},
},
modified: map[string]string{
"file1.txt": "modified content",
},
expectOutput: "",
expectError: true,
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

// Set up command error output
var output strings.Builder
tt.helper.Cmd.SetErr(&output)

// Call the CheckModified method
err := tt.helper.CheckModified(tt.modified)

// Check if error status matches expectation
if tt.expectError {
assert.Error(t, err)
assert.Equal(t, "files were modified", err.Error())
} else {
assert.NoError(t, err)
}

// Check if output contains expected strings
if tt.expectOutput != "" {
// We can't guarantee the order of map iteration, so we check that
// the output contains each expected line
for path := range tt.modified {
if !tt.helper.Quiet {
assert.Contains(t, output.String(), "Modified: "+path)
}
}
}
})
}
}

func TestProcessOutput(t *testing.T) {
t.Parallel()

Expand Down
0