Skip to content

Commit d31662d

Browse files
committed
Rework test scripts
1 parent 1b4865c commit d31662d

4 files changed

Lines changed: 98 additions & 112 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
test:
1010
strategy:
1111
matrix:
12-
go-version: [1.19.x]
12+
go-version: [1.19.x,1.20.x]
1313
platform: [macos-latest, ubuntu-latest, windows-latest]
1414
runs-on: ${{ matrix.platform }}
1515
steps:

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require (
1212
github.com/frankban/quicktest v1.14.3
1313
github.com/oklog/ulid/v2 v2.1.0
1414
github.com/peterbourgon/ff/v3 v3.3.0
15-
github.com/rogpeppe/go-internal v1.10.0
16-
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0
15+
github.com/rogpeppe/go-internal v1.10.1-0.20230506080601-b93e0026e77b
16+
golang.org/x/sync v0.1.0
1717
golang.org/x/text v0.8.0
1818
gopkg.in/yaml.v2 v2.4.0
1919
)
@@ -33,5 +33,6 @@ require (
3333
github.com/jmespath/go-jmespath v0.4.0 // indirect
3434
github.com/kr/pretty v0.3.0 // indirect
3535
github.com/kr/text v0.2.0 // indirect
36-
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
36+
golang.org/x/sys v0.5.0 // indirect
37+
golang.org/x/tools v0.6.0 // indirect
3738
)

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNs
5757
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
5858
github.com/peterbourgon/ff/v3 v3.3.0 h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24=
5959
github.com/peterbourgon/ff/v3 v3.3.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
60-
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
61-
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
6260
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6361
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6462
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
65-
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
66-
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
67-
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
68-
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
63+
github.com/rogpeppe/go-internal v1.10.1-0.20230506080601-b93e0026e77b h1:a7YzBJGU2SWAAcUEUNz8Qm1RPGv/wo4KisXDF/WMGTw=
64+
github.com/rogpeppe/go-internal v1.10.1-0.20230506080601-b93e0026e77b/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
6965
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
70-
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
71-
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
66+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
67+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
68+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
69+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7270
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
7371
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
72+
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
73+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
7474
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7575
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7676
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=

main_test.go

Lines changed: 85 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
package main
77

88
import (
9+
"bytes"
910
"fmt"
10-
"log"
1111
"net/http"
1212
"os"
1313
"sort"
14-
"strconv"
1514
"strings"
1615
"testing"
17-
"time"
1816

1917
"github.com/oklog/ulid/v2"
2018

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

3028
}
31-
testscript.Run(t, testscript.Params{
32-
Dir: "testscripts",
33-
//UpdateScripts: true,
34-
Setup: func(env *testscript.Env) error {
35-
return setup(env)
36-
},
37-
})
29+
p := commonTestScriptsParam
30+
p.Dir = "testscripts"
31+
testscript.Run(t, p)
3832
}
3933

4034
// Tests in development can be put in "testscripts/unfinished".
4135
func TestUnfinished(t *testing.T) {
4236
if os.Getenv("CI") != "" {
4337
t.Skip("skip unfinished tests on CI")
4438
}
39+
p := commonTestScriptsParam
40+
p.Dir = "testscripts/unfinished"
41+
testscript.Run(t, p)
4542

46-
testscript.Run(t, testscript.Params{
47-
Dir: "testscripts/unfinished",
48-
//TestWork: true,
49-
//UpdateScripts: true,
50-
Setup: func(env *testscript.Env) error {
51-
return setup(env)
52-
},
53-
})
5443
}
5544

5645
func TestMain(m *testing.M) {
@@ -64,88 +53,6 @@ func TestMain(m *testing.M) {
6453
}
6554
return 0
6655
},
67-
68-
// head executes HTTP HEAD on the given URL and prints the response status code and
69-
// headers to stdout.
70-
"head": func() int {
71-
url := s3IntegrationTestHttpRoot + os.Args[1]
72-
fmt.Println("head", url)
73-
resp, err := http.DefaultClient.Head(url)
74-
if err != nil {
75-
log.Fatal(err)
76-
}
77-
path := strings.ReplaceAll(os.Args[1], os.Getenv("S3DEPLOY_TEST_ID"), "S3DEPLOY_TEST_ID")
78-
fmt.Printf("Head: %s;Status: %d;", path, resp.StatusCode)
79-
// Print headers
80-
var headers []string
81-
for k, v := range resp.Header {
82-
headers = append(headers, fmt.Sprintf("%s: %s", k, v[0]))
83-
}
84-
sort.Strings(headers)
85-
fmt.Printf("Headers: %s", strings.Join(headers, ";"))
86-
87-
return 0
88-
},
89-
90-
// append appends to a file with a leaading newline.
91-
"append": func() int {
92-
if len(os.Args) < 3 {
93-
94-
fmt.Fprintln(os.Stderr, "usage: append FILE TEXT")
95-
return 1
96-
}
97-
98-
filename := os.Args[1]
99-
words := os.Args[2:]
100-
for i, word := range words {
101-
words[i] = strings.Trim(word, "\"")
102-
}
103-
text := strings.Join(words, " ")
104-
105-
_, err := os.Stat(filename)
106-
if err != nil {
107-
if os.IsNotExist(err) {
108-
fmt.Fprintln(os.Stderr, "file does not exist:", filename)
109-
return 1
110-
}
111-
fmt.Fprintln(os.Stderr, err)
112-
return 1
113-
}
114-
115-
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o644)
116-
if err != nil {
117-
fmt.Fprintln(os.Stderr, "failed to open file:", filename)
118-
return 1
119-
}
120-
defer f.Close()
121-
122-
_, err = f.WriteString("\n" + text)
123-
if err != nil {
124-
fmt.Fprintln(os.Stderr, "failed to write to file:", filename)
125-
return 1
126-
}
127-
128-
return 0
129-
},
130-
131-
// log prints to stderr.
132-
"log": func() int {
133-
log.Println(os.Args[1])
134-
return 0
135-
},
136-
137-
"sleep": func() int {
138-
i, err := strconv.Atoi(os.Args[1])
139-
if err != nil {
140-
i = 1
141-
}
142-
time.Sleep(time.Duration(i) * time.Second)
143-
return 0
144-
},
145-
"unset": func() int {
146-
os.Unsetenv(os.Args[1])
147-
return 0
148-
},
14956
}),
15057
)
15158
}
@@ -159,3 +66,81 @@ func setup(env *testscript.Env) error {
15966
env.Setenv("S3DEPLOY_TEST_ID", strings.ToLower(ulid.Make().String()))
16067
return nil
16168
}
69+
70+
var commonTestScriptsParam = testscript.Params{
71+
Setup: func(env *testscript.Env) error {
72+
return setup(env)
73+
},
74+
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
75+
76+
// head executes HTTP HEAD on the given URL and prints the response status code and
77+
// headers to stdout.
78+
"head": func(ts *testscript.TestScript, neg bool, args []string) {
79+
url := s3IntegrationTestHttpRoot + args[0]
80+
fmt.Fprintln(ts.Stdout(), "head", url)
81+
resp, err := http.DefaultClient.Head(url)
82+
if err != nil {
83+
ts.Fatalf("failed to HEAD %s: %v", url, err)
84+
}
85+
path := strings.ReplaceAll(args[0], ts.Getenv("S3DEPLOY_TEST_ID"), "S3DEPLOY_TEST_ID")
86+
fmt.Fprintf(ts.Stdout(), "Head: %s;Status: %d;", path, resp.StatusCode)
87+
// Print headers
88+
var headers []string
89+
for k, v := range resp.Header {
90+
headers = append(headers, fmt.Sprintf("%s: %s", k, v[0]))
91+
}
92+
sort.Strings(headers)
93+
fmt.Fprintf(ts.Stdout(), "Headers: %s", strings.Join(headers, ";"))
94+
95+
},
96+
97+
// append appends to a file with a leaading newline.
98+
"append": func(ts *testscript.TestScript, neg bool, args []string) {
99+
if len(args) < 2 {
100+
ts.Fatalf("usage: append FILE TEXT")
101+
}
102+
103+
filename := ts.MkAbs(args[0])
104+
words := args[1:]
105+
for i, word := range words {
106+
words[i] = strings.Trim(word, "\"")
107+
}
108+
text := strings.Join(words, " ")
109+
110+
_, err := os.Stat(filename)
111+
if err != nil {
112+
if os.IsNotExist(err) {
113+
ts.Fatalf("file does not exist: %s", filename)
114+
}
115+
ts.Fatalf("failed to stat file: %v", err)
116+
}
117+
118+
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o644)
119+
if err != nil {
120+
ts.Fatalf("failed to open file: %v", err)
121+
}
122+
defer f.Close()
123+
124+
_, err = f.WriteString("\n" + text)
125+
if err != nil {
126+
ts.Fatalf("failed to write to file: %v", err)
127+
}
128+
},
129+
// replace replaces a string in a file.
130+
"replace": func(ts *testscript.TestScript, neg bool, args []string) {
131+
if len(args) < 3 {
132+
ts.Fatalf("usage: replace FILE OLD NEW")
133+
}
134+
filename := ts.MkAbs(args[0])
135+
oldContent, err := os.ReadFile(filename)
136+
if err != nil {
137+
ts.Fatalf("failed to read file %v", err)
138+
}
139+
newContent := bytes.Replace(oldContent, []byte(args[1]), []byte(args[2]), -1)
140+
err = os.WriteFile(filename, newContent, 0o644)
141+
if err != nil {
142+
ts.Fatalf("failed to write file: %v", err)
143+
}
144+
},
145+
},
146+
}

0 commit comments

Comments
 (0)