-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
142 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# auto-generated by scripts/dependabot. DO NOT EDIT. | ||
name: golden | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
paths: | ||
- exp/golden/** | ||
- .github/workflows/golden.yml | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
runs-on: ${{ matrix.os }} | ||
defaults: | ||
run: | ||
working-directory: ./exp/golden | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: ./exp/golden/go.mod | ||
cache: true | ||
cache-dependency-path: ./exp/golden.sum | ||
- run: go build -v ./... | ||
- run: go test -race -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/charmbracelet/x/exp/golden | ||
|
||
go 1.19 | ||
|
||
require github.com/aymanbagabas/go-udiff v0.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= | ||
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package golden | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/aymanbagabas/go-udiff" | ||
) | ||
|
||
var update = flag.Bool("update", false, "update .golden files") | ||
|
||
// RequireEqual is a helper function to assert the given output is | ||
// the expected from the golden files, printing its diff in case it is not. | ||
// | ||
// You can update the golden files by running your tests with the -update flag. | ||
func RequireEqual(tb testing.TB, out []byte) { | ||
tb.Helper() | ||
|
||
out = fixLineEndings(out) | ||
|
||
golden := filepath.Join("testdata", tb.Name()+".golden") | ||
if *update { | ||
if err := os.MkdirAll(filepath.Dir(golden), 0o755); err != nil { //nolint: gomnd | ||
tb.Fatal(err) | ||
} | ||
if err := os.WriteFile(golden, out, 0o600); err != nil { //nolint: gomnd | ||
tb.Fatal(err) | ||
} | ||
} | ||
|
||
path := filepath.Join(tb.TempDir(), tb.Name()+".out") | ||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { //nolint: gomnd | ||
tb.Fatal(err) | ||
} | ||
if err := os.WriteFile(path, out, 0o600); err != nil { //nolint: gomnd | ||
tb.Fatal(err) | ||
} | ||
|
||
goldenBts, err := os.ReadFile(golden) | ||
if err != nil { | ||
tb.Fatal(err) | ||
} | ||
|
||
goldenBts = fixLineEndings(goldenBts) | ||
diff := udiff.Unified("golden", "run", string(goldenBts), string(out)) | ||
if diff != "" { | ||
tb.Fatalf("output does not match, expected:\n\n%s\n\ngot:\n\n%s\n\ndiff:\n\n%s", string(goldenBts), string(out), diff) | ||
} | ||
} | ||
|
||
func fixLineEndings(in []byte) []byte { | ||
if runtime.GOOS != "windows" { | ||
return in | ||
} | ||
return bytes.ReplaceAll(in, []byte("\r\n"), []byte{'\n'}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package golden | ||
|
||
import "testing" | ||
|
||
func TestRequireEqualUpdate(t *testing.T) { | ||
enableUpdate(t) | ||
RequireEqual(t, []byte("test")) | ||
} | ||
|
||
func TestRequireEqualNoUpdate(t *testing.T) { | ||
RequireEqual(t, []byte("test")) | ||
} | ||
|
||
func enableUpdate(tb testing.TB) { | ||
tb.Helper() | ||
previous := update | ||
*update = true | ||
tb.Cleanup(func() { | ||
update = previous | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ go 1.21 | |
use ( | ||
./editor | ||
./errors | ||
./exp/golden | ||
./exp/higherorder | ||
./exp/ordered | ||
./exp/slice | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters