Skip to content

Commit

Permalink
chore: retrieve UX improvements from IPFS Thing 2023 workshop setup (#95
Browse files Browse the repository at this point in the history
)

* chore: retrieve UX improvements from IPFS Thing 2023 workshop setup

* chore: replace contains with has prefix checks

* docs: update CHANGELOG
  • Loading branch information
galargh authored Jun 30, 2023
1 parent abd68cc commit 5b87eeb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- `--verbose` flag displays all the output to the console

## [0.2.0] - 2023-06-26
### Added
Expand Down
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
all: test-kubo

test-cargateway: provision-cargateway fixtures.car gateway-conformance
./gateway-conformance test --json output.json --gateway-url http://127.0.0.1:8040 --specs -subdomain-gateway
./gateway-conformance test --json reports/output.json --gateway-url http://127.0.0.1:8040 --specs -subdomain-gateway

test-kubo-subdomains: provision-kubo gateway-conformance
./kubo-config.example.sh
./gateway-conformance test --json output.json --gateway-url http://127.0.0.1:8080 --subdomain-url http://example.com:8080
./gateway-conformance test --json reports/output.json --gateway-url http://127.0.0.1:8080 --subdomain-url http://example.com:8080

test-kubo: provision-kubo fixtures.car gateway-conformance
./gateway-conformance test --json output.json --gateway-url http://127.0.0.1:8080 --specs -subdomain-gateway
test-kubo: provision-kubo gateway-conformance
./gateway-conformance test --json reports/output.json --gateway-url http://127.0.0.1:8080 --specs -subdomain-gateway

provision-cargateway: ./fixtures.car
# cd go-libipfs/examples/car && go install
car -c ./fixtures.car &

provision-kubo: fixtures.car
provision-kubo:
find ./fixtures -name '*.car' -exec ipfs dag import {} \;
find ./fixtures -name '*.ipns-record' -exec sh -c 'ipfs routing put --allow-offline /ipns/$$(basename -s .ipns-record "{}") "{}"' \;

# tools
fixtures.car: gateway-conformance
./gateway-conformance extract-fixtures --merged=true --dir=.

gateway-conformance:
go build -o ./gateway-conformance ./cmd/gateway-conformance

test-docker: fixtures.car gateway-conformance
docker build -t gateway-conformance .
docker run --rm -v "${PWD}:/workspace" -w "/workspace" --network=host gateway-conformance test
test-docker: docker fixtures.car gateway-conformance
./gc test

output.xml: test-kubo
docker run --rm -v "${PWD}:/workspace" -w "/workspace" --entrypoint "/bin/bash" ghcr.io/pl-strflt/saxon:v1 -c """
Expand All @@ -37,4 +37,7 @@ output.html: output.xml
docker run --rm -v "${PWD}:/workspace" -w "/workspace" ghcr.io/pl-strflt/saxon:v1 -s:output.xml -xsl:/etc/junit-noframes-saxon.xsl -o:output.html
open ./output.html

docker:
docker build -t gateway-conformance .

.PHONY: gateway-conformance
58 changes: 56 additions & 2 deletions cmd/gateway-conformance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ type event struct {

type out struct {
Writer io.Writer
Filter func(s string) bool
}

func (o out) Write(p []byte) (n int, err error) {
os.Stdout.Write(p)
if o.Filter != nil {
for _, line := range strings.Split(string(p), "\n") {
if o.Filter(line) {
os.Stdout.Write([]byte(fmt.Sprintf("%s\n", line)))
}
}
}
return o.Writer.Write(p)
}

Expand All @@ -52,6 +59,7 @@ func copyFiles(inputPaths []string, outputDirectoryPath string) error {
newName := fmt.Sprintf("%s_%d%s", name, i, ext)

outputPath := filepath.Join(outputDirectoryPath, newName)

dst, err := os.Create(outputPath)
if err != nil {
return err
Expand All @@ -72,6 +80,7 @@ func main() {
var specs string
var directory string
var merged bool
var verbose bool

app := &cli.App{
Name: "gateway-conformance",
Expand Down Expand Up @@ -108,6 +117,12 @@ func main() {
Value: "",
Destination: &specs,
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Prints all the output to the console.",
Value: false,
Destination: &verbose,
},
},
Action: func(cCtx *cli.Context) error {
args := []string{"test", "./tests", "-test.v=test2json"}
Expand All @@ -129,20 +144,58 @@ func main() {
cmd.Env = append(cmd.Env, fmt.Sprintf("SUBDOMAIN_GATEWAY_URL=%s", subdomainGatewayURL))
}

cmd.Stdout = out{output}
cmd.Stdout = out{
Writer: output,
Filter: func(line string) bool {
return verbose ||
strings.HasPrefix(line, "\u0016FAIL") ||
strings.HasPrefix(line, "\u0016--- FAIL") ||
strings.HasPrefix(line, "\u0016PASS")
},
}
cmd.Stderr = os.Stderr

fmt.Println("Running tests...\n")
testErr := cmd.Run()
fmt.Println("\nDONE!\n")

if testErr != nil {
fmt.Println("\nLooking for details...\n")
strOutput := output.String()
lineDump := []string{}
for _, line := range strings.Split(strOutput, "\n") {
if strings.HasPrefix(line, "\u0016FAIL") || strings.HasPrefix(line, "\u0016--- FAIL") {
fmt.Println(line)
for _, l := range lineDump {
fmt.Println(l)
}
lineDump = []string{}
} else if strings.HasPrefix(line, "\u0016===") {
lineDump = []string{}
} else {
lineDump = append(lineDump, line)
}
}
fmt.Println("\nDONE!\n")
}

if jsonOutput != "" {
json := &bytes.Buffer{}
cmd = exec.Command("go", "tool", "test2json", "-p", "Gateway Tests", "-t")
cmd.Stdin = output
cmd.Stdout = json
cmd.Stderr = os.Stderr

fmt.Println("\nGenerating JSON report...")
err := cmd.Run()
if err != nil {
return err
}
// create directory if it doesn't exist
err = os.MkdirAll(filepath.Dir(jsonOutput), 0755)
if err != nil {
return err
}
// write jsonOutput to json file
f, err := os.Create(jsonOutput)
if err != nil {
Expand All @@ -153,6 +206,7 @@ func main() {
if err != nil {
return err
}
fmt.Println("DONE!\n")
}

return testErr
Expand Down
3 changes: 3 additions & 0 deletions gc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker run --rm -v "${PWD}:/workspace" -w "/workspace" --network=host gateway-conformance "$@"
11 changes: 6 additions & 5 deletions tooling/test/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"testing"
Expand All @@ -22,15 +23,15 @@ Hint: {{.Test.Hint}}
Error: {{.Err}}
Request:
Expected Request:
{{.Test.Request | json}}
Expected Response:
{{.Test.Response | json}}
Actual Request:
{{.Req | dump}}
Expected Response:
{{.Test.Response | json}}
Actual Response:
{{.Res | dump}}
`
Expand Down Expand Up @@ -91,7 +92,7 @@ func report(t *testing.T, test SugarTest, req *http.Request, res *http.Response,
var buf bytes.Buffer
err = tmpl.Execute(&buf, input)
if err != nil {
panic(err)
panic(fmt.Errorf("failed to execute template: %#v %w", input, err))
}

if input.Err != nil {
Expand Down

0 comments on commit 5b87eeb

Please sign in to comment.