Skip to content

Commit

Permalink
Merge branch 'master' into feat-url-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmutant authored Feb 18, 2021
2 parents 40936f2 + 4848d14 commit 3f9a86a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: CI
strategy:
matrix:
go: ['1.13', '1.14', '1.15']
go: ['1.14', '1.15', '1.16']
os: ['ubuntu-latest', 'windows-latest', 'macOS-latest']
runs-on: ${{ matrix.os }}
steps:
Expand Down
8 changes: 6 additions & 2 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func checkTB(tb tb, prop func(*T)) {
} else {
failfile := failFileName(tb.Name())
out := captureTestOutput(tb, prop, buf)
err := saveFailFile(failfile, out, buf)
err := saveFailFile(failfile, rapidVersion, out, buf)
if err == nil {
repr = fmt.Sprintf("-rapid.failfile=%q", failfile)
} else {
Expand Down Expand Up @@ -195,11 +195,15 @@ func doCheck(tb tb, failfile string, checks int, seed uint64, prop func(*T)) (in
func checkFailFile(tb tb, failfile string, prop func(*T)) ([]uint64, *testError, *testError) {
tb.Helper()

buf, err := loadFailFile(failfile)
version, buf, err := loadFailFile(failfile)
if err != nil {
tb.Logf("[rapid] ignoring fail file: %v", err)
return nil, nil, nil
}
if version != rapidVersion {
tb.Logf("[rapid] ignoring fail file: version %q differs from rapid version %q", version, rapidVersion)
return nil, nil, nil
}

s1 := newBufBitStream(buf, false)
t1 := newT(tb, s1, flags.verbose, nil)
Expand Down
16 changes: 10 additions & 6 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

package rapid

import "reflect"
import (
"reflect"
"sync"
)

type value interface{}

Expand All @@ -17,9 +20,10 @@ type generatorImpl interface {
}

type Generator struct {
impl generatorImpl
typ reflect.Type
str string
impl generatorImpl
typ reflect.Type
strOnce sync.Once
str string
}

func newGenerator(impl generatorImpl) *Generator {
Expand All @@ -30,9 +34,9 @@ func newGenerator(impl generatorImpl) *Generator {
}

func (g *Generator) String() string {
if g.str == "" {
g.strOnce.Do(func() {
g.str = g.impl.String()
}
})

return g.str
}
Expand Down
24 changes: 15 additions & 9 deletions persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
)

const (
rapidVersion = "v0.4.4"

persistDirMode = 0775
failfileTmpPattern = ".rapid-failfile-tmp-*"
)
Expand All @@ -40,7 +42,7 @@ func failFileName(testName string) string {
return fmt.Sprintf("%s-%s-%d.fail", kindaSafeFilename(testName), ts, os.Getpid())
}

func saveFailFile(filename string, output []byte, buf []uint64) error {
func saveFailFile(filename string, version string, output []byte, buf []uint64) error {
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, persistDirMode)
if err != nil {
Expand All @@ -62,7 +64,7 @@ func saveFailFile(filename string, output []byte, buf []uint64) error {
}
}

var bs []string
bs := []string{version}
for _, u := range buf {
bs = append(bs, fmt.Sprintf("0x%x", u))
}
Expand All @@ -81,10 +83,10 @@ func saveFailFile(filename string, output []byte, buf []uint64) error {
return nil
}

func loadFailFile(filename string) ([]uint64, error) {
func loadFailFile(filename string) (string, []uint64, error) {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open fail file: %w", err)
return "", nil, fmt.Errorf("failed to open fail file: %w", err)
}
defer func() { _ = f.Close() }()

Expand All @@ -98,18 +100,22 @@ func loadFailFile(filename string) ([]uint64, error) {
data = s
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed to load fail file %q: %w", filename, err)
return "", nil, fmt.Errorf("failed to load fail file %q: %w", filename, err)
}

var buf []uint64
fields := strings.Fields(data)
for _, b := range fields {
if len(fields) == 0 {
return "", nil, fmt.Errorf("no data in fail file %q", filename)
}

var buf []uint64
for _, b := range fields[1:] {
u, err := strconv.ParseUint(b, 0, 64)
if err != nil {
return nil, fmt.Errorf("failed to load fail file %q: %w", filename, err)
return "", nil, fmt.Errorf("failed to load fail file %q: %w", filename, err)
}
buf = append(buf, u)
}

return buf, nil
return fields[0], buf, nil
}
11 changes: 8 additions & 3 deletions persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@ func TestFailFileRoundtrip(t *testing.T) {

Check(t, func(t *T) {
var (
testName = String().Draw(t, "testName").(string)
// OS X seems to have issues with Go 1.16 and String(), reporting "illegal byte sequence" when trying to rename the file
testName = StringMatching(`[a-zA-Z0-9._-]+`).Draw(t, "testName").(string)
version = StringMatching(`[a-zA-Z0-9._-]+`).Draw(t, "version").(string)
output = SliceOf(Byte()).Draw(t, "output").([]byte)
buf = SliceOf(Uint64()).Draw(t, "buf").([]uint64)
)

fileName := failFileName(testName)
err := saveFailFile(fileName, output, buf)
err := saveFailFile(fileName, version, output, buf)
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Remove(fileName) }()

buf2, err := loadFailFile(fileName)
version2, buf2, err := loadFailFile(fileName)
if err != nil {
t.Fatal(err)
}

if version2 != version {
t.Fatalf("got version %q instead of %q", version2, version)
}
if len(buf2) != len(buf) {
t.Fatalf("got buf of length %v instead of %v", len(buf2), len(buf))
}
Expand Down
10 changes: 5 additions & 5 deletions strings_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

// String generation depends on the Unicode tables, which change with Go versions:
// +build go1.14
// +build go1.16

package rapid_test

Expand All @@ -31,8 +31,8 @@ func ExampleRune() {
// '\\' '\ufeff' '?' '~' '-'
// '0' '$' '!' '`' '\ue05d'
// '"' '&' '#' '\u0604' 'A'
// '&' '' '@' '#' '|'
// '⊙' '𝩔' '$' '҈' '\r'
// '&' '' '@' '#' '|'
// '⊙' '𝩇' '$' '҈' '\r'
}

func ExampleRuneFrom() {
Expand Down Expand Up @@ -67,7 +67,7 @@ func ExampleString() {
}
// Output:
// "\\߾⃝!/?Ⱥ֍"
// "\u2006𑨷"
// "\u2006𑨃"
// "?﹩\u0603ᾢ"
// ".*%:<%৲"
// ""
Expand Down Expand Up @@ -95,7 +95,7 @@ func ExampleStringN() {
}
// Output:
// "\\߾⃝!/"
// "\u2006𑨷%\v\ufeff"
// "\u2006𑨃%\v\ufeff"
// "?﹩\u0603ᾢÉ"
// ".*%:<"
// ":?\"~¤"
Expand Down

0 comments on commit 3f9a86a

Please sign in to comment.