Skip to content

Commit

Permalink
Merge pull request #5 from customerio/make-accept-changes-work-for-an…
Browse files Browse the repository at this point in the history
…y-extension
  • Loading branch information
avigoldman authored Nov 18, 2024
2 parents b61e3f4 + e329e65 commit b620b1f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
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

0 comments on commit b620b1f

Please sign in to comment.