Skip to content

Commit

Permalink
Rework test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 6, 2023
1 parent 1b4865c commit d31662d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.19.x]
go-version: [1.19.x,1.20.x]
platform: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ require (
github.com/frankban/quicktest v1.14.3
github.com/oklog/ulid/v2 v2.1.0
github.com/peterbourgon/ff/v3 v3.3.0
github.com/rogpeppe/go-internal v1.10.0
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0
github.com/rogpeppe/go-internal v1.10.1-0.20230506080601-b93e0026e77b
golang.org/x/sync v0.1.0
golang.org/x/text v0.8.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -33,5 +33,6 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/tools v0.6.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNs
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/peterbourgon/ff/v3 v3.3.0 h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24=
github.com/peterbourgon/ff/v3 v3.3.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.10.1-0.20230506080601-b93e0026e77b h1:a7YzBJGU2SWAAcUEUNz8Qm1RPGv/wo4KisXDF/WMGTw=
github.com/rogpeppe/go-internal v1.10.1-0.20230506080601-b93e0026e77b/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
185 changes: 85 additions & 100 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
package main

import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
"testing"
"time"

"github.com/oklog/ulid/v2"

Expand All @@ -28,29 +26,20 @@ func TestIntegration(t *testing.T) {
t.Skip("S3DEPLOY_TEST_KEY not set")

}
testscript.Run(t, testscript.Params{
Dir: "testscripts",
//UpdateScripts: true,
Setup: func(env *testscript.Env) error {
return setup(env)
},
})
p := commonTestScriptsParam
p.Dir = "testscripts"
testscript.Run(t, p)
}

// Tests in development can be put in "testscripts/unfinished".
func TestUnfinished(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("skip unfinished tests on CI")
}
p := commonTestScriptsParam
p.Dir = "testscripts/unfinished"
testscript.Run(t, p)

testscript.Run(t, testscript.Params{
Dir: "testscripts/unfinished",
//TestWork: true,
//UpdateScripts: true,
Setup: func(env *testscript.Env) error {
return setup(env)
},
})
}

func TestMain(m *testing.M) {
Expand All @@ -64,88 +53,6 @@ func TestMain(m *testing.M) {
}
return 0
},

// head executes HTTP HEAD on the given URL and prints the response status code and
// headers to stdout.
"head": func() int {
url := s3IntegrationTestHttpRoot + os.Args[1]
fmt.Println("head", url)
resp, err := http.DefaultClient.Head(url)
if err != nil {
log.Fatal(err)
}
path := strings.ReplaceAll(os.Args[1], os.Getenv("S3DEPLOY_TEST_ID"), "S3DEPLOY_TEST_ID")
fmt.Printf("Head: %s;Status: %d;", path, resp.StatusCode)
// Print headers
var headers []string
for k, v := range resp.Header {
headers = append(headers, fmt.Sprintf("%s: %s", k, v[0]))
}
sort.Strings(headers)
fmt.Printf("Headers: %s", strings.Join(headers, ";"))

return 0
},

// append appends to a file with a leaading newline.
"append": func() int {
if len(os.Args) < 3 {

fmt.Fprintln(os.Stderr, "usage: append FILE TEXT")
return 1
}

filename := os.Args[1]
words := os.Args[2:]
for i, word := range words {
words[i] = strings.Trim(word, "\"")
}
text := strings.Join(words, " ")

_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "file does not exist:", filename)
return 1
}
fmt.Fprintln(os.Stderr, err)
return 1
}

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to open file:", filename)
return 1
}
defer f.Close()

_, err = f.WriteString("\n" + text)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to write to file:", filename)
return 1
}

return 0
},

// log prints to stderr.
"log": func() int {
log.Println(os.Args[1])
return 0
},

"sleep": func() int {
i, err := strconv.Atoi(os.Args[1])
if err != nil {
i = 1
}
time.Sleep(time.Duration(i) * time.Second)
return 0
},
"unset": func() int {
os.Unsetenv(os.Args[1])
return 0
},
}),
)
}
Expand All @@ -159,3 +66,81 @@ func setup(env *testscript.Env) error {
env.Setenv("S3DEPLOY_TEST_ID", strings.ToLower(ulid.Make().String()))
return nil
}

var commonTestScriptsParam = testscript.Params{
Setup: func(env *testscript.Env) error {
return setup(env)
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){

// head executes HTTP HEAD on the given URL and prints the response status code and
// headers to stdout.
"head": func(ts *testscript.TestScript, neg bool, args []string) {
url := s3IntegrationTestHttpRoot + args[0]
fmt.Fprintln(ts.Stdout(), "head", url)
resp, err := http.DefaultClient.Head(url)
if err != nil {
ts.Fatalf("failed to HEAD %s: %v", url, err)
}
path := strings.ReplaceAll(args[0], ts.Getenv("S3DEPLOY_TEST_ID"), "S3DEPLOY_TEST_ID")
fmt.Fprintf(ts.Stdout(), "Head: %s;Status: %d;", path, resp.StatusCode)
// Print headers
var headers []string
for k, v := range resp.Header {
headers = append(headers, fmt.Sprintf("%s: %s", k, v[0]))
}
sort.Strings(headers)
fmt.Fprintf(ts.Stdout(), "Headers: %s", strings.Join(headers, ";"))

},

// append appends to a file with a leaading newline.
"append": func(ts *testscript.TestScript, neg bool, args []string) {
if len(args) < 2 {
ts.Fatalf("usage: append FILE TEXT")
}

filename := ts.MkAbs(args[0])
words := args[1:]
for i, word := range words {
words[i] = strings.Trim(word, "\"")
}
text := strings.Join(words, " ")

_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
ts.Fatalf("file does not exist: %s", filename)
}
ts.Fatalf("failed to stat file: %v", err)
}

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
ts.Fatalf("failed to open file: %v", err)
}
defer f.Close()

_, err = f.WriteString("\n" + text)
if err != nil {
ts.Fatalf("failed to write to file: %v", err)
}
},
// replace replaces a string in a file.
"replace": func(ts *testscript.TestScript, neg bool, args []string) {
if len(args) < 3 {
ts.Fatalf("usage: replace FILE OLD NEW")
}
filename := ts.MkAbs(args[0])
oldContent, err := os.ReadFile(filename)
if err != nil {
ts.Fatalf("failed to read file %v", err)
}
newContent := bytes.Replace(oldContent, []byte(args[1]), []byte(args[2]), -1)
err = os.WriteFile(filename, newContent, 0o644)
if err != nil {
ts.Fatalf("failed to write file: %v", err)
}
},
},
}

0 comments on commit d31662d

Please sign in to comment.