Skip to content
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

make AcceptChanges work for any extension #5

Merged
merged 1 commit into from
Nov 18, 2024
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
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ApprovalTests for [go](https://golang.org/)
ApprovalTests allows for easy testing of larger objects, strings and anything else that can be saved to a file (images, sounds, csv, etc...)

# Examples

## Basic string verification

```go
Expand All @@ -29,22 +30,47 @@ func TestHelloWorld(t *testing.T) {
```

## Store approved files in testdata subfolder
Some people prefer to store their approved files in a subfolder "testdata" instead of in the same folder as the

Some people prefer to store their approved files in a subfolder "testdata" instead of in the same folder as the
production code. To configure this, add a call to UseFolder to your TestMain:

```go
func TestMain(m *testing.M) {
UseFolder("testdata")
approvals.UseFolder("testdata")
os.Exit(m.Run())
}
```

## Accept the changes in the output

If you have made changes to the output and want to accept them, you can use the `AcceptChanges` function:

```go
func TestMain(m *testing.M) {
approvals.AcceptChanges(true)
}
```

A good pattern is to use a command line flag to control this behavior, such as `-u` for update:

```go
var update = flag.Bool("u", false, "update .approved files")

func TestMain(m *testing.M) {
flag.Parse()
approvals.AcceptChanges(*update)
os.Exit(m.Run())
}
```

## In Project

Note: ApprovalTests uses approvals to test itself. Therefore there are many examples in the code itself.

- [approvals_test.go](approvals_test.go)

## JSON

VerifyJSONBytes - Simple Formatting for easy comparison. Also uses the .json file extension

```go
Expand All @@ -65,6 +91,7 @@ Matches file: approvals_test.TestVerifyJSON.received.json
```

## Reporters

ApprovalTests becomes _much_ more powerful with reporters. Reporters launch programs on failure to help you understand, fix and approve results.

You can make your own easily, [here's an example](reporters/beyond_compare.go)
Expand Down
21 changes: 6 additions & 15 deletions approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func alwaysOption(opts []verifyOptions) verifyOptions {
}

// AcceptChanges handles renaming `.received` files to `.approved` based on the given flag.
// If `approve` is true, it renames all `.received.txt` and `.received.json` files in the default folder
// to `.approved.txt` and `.approved.json`, respectively.
// If `approve` is true, it renames all `.received.*` files and `.received` files in the default folder
// to `.approved.*` and `.approved`, respectively.
func AcceptChanges(approve bool) error {
if !approve {
return nil
Expand All @@ -332,22 +332,13 @@ func AcceptChanges(approve bool) error {
return nil
}

// Handle `.received.txt` files
if strings.HasSuffix(info.Name(), ".received.txt") {
approvedName := strings.TrimSuffix(path, "received.txt") + "approved.txt"
// Handle `.received.*` files and `.received` files
if strings.Contains(info.Name(), ".received") {
approvedName := strings.Replace(path, ".received", ".approved", 1)
if err := os.Rename(path, approvedName); err != nil {
return fmt.Errorf("failed to rename %s to %s: %w", path, approvedName, err)
}
fmt.Printf("Renamed %s to %s\n", path, approvedName)
}

// Handle `.received.json` files
if strings.HasSuffix(info.Name(), ".received.json") {
approvedName := strings.TrimSuffix(path, "received.json") + "approved.json"
if err := os.Rename(path, approvedName); err != nil {
return fmt.Errorf("failed to rename %s to %s: %w", path, approvedName, err)
}
fmt.Printf("Renamed %s to %s\n", path, approvedName)
fmt.Printf("Accepted changes to %s\n", approvedName)
}

return nil
Expand Down