Merge pull request #310 from nhooyr/testdata-2322

lib/diff: Delete in favour of util-go
This commit is contained in:
Anmol Sethi 2022-12-01 10:27:45 -08:00 committed by GitHub
commit 83872ee1c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 68 deletions

View file

@ -12,9 +12,10 @@ import (
"cdr.dev/slog"
"github.com/stretchr/testify/assert"
tassert "github.com/stretchr/testify/assert"
"oss.terrastruct.com/diff"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
@ -22,7 +23,6 @@ import (
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2target"
xdiff "oss.terrastruct.com/d2/lib/diff"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
)
@ -90,7 +90,7 @@ func run(t *testing.T, tc testCase) {
ctx = log.Leveled(ctx, slog.LevelDebug)
ruler, err := textmeasure.NewRuler()
if !assert.Nil(t, err) {
if !tassert.Nil(t, err) {
return
}
@ -110,7 +110,7 @@ func run(t *testing.T, tc testCase) {
ThemeID: 0,
Layout: layout,
})
if !assert.Nil(t, err) {
if !tassert.Nil(t, err) {
return
}
@ -122,35 +122,24 @@ func run(t *testing.T, tc testCase) {
dataPath := filepath.Join("testdata", strings.TrimPrefix(t.Name(), "TestE2E/"), layoutName)
pathGotSVG := filepath.Join(dataPath, "sketch.got.svg")
pathExpSVG := filepath.Join(dataPath, "sketch.exp.svg")
svgBytes, err := d2svg.Render(diagram)
if err != nil {
t.Fatal(err)
}
assert.Success(t, err)
err = ioutil.WriteFile(pathGotSVG, svgBytes, 0600)
assert.Success(t, err)
defer os.Remove(pathGotSVG)
var xmlParsed interface{}
if err := xml.Unmarshal(svgBytes, &xmlParsed); err != nil {
t.Fatalf("invalid SVG: %v", err)
}
err = xml.Unmarshal(svgBytes, &xmlParsed)
assert.Success(t, err)
err = diff.Testdata(filepath.Join(dataPath, "board"), diagram)
if err != nil {
ioutil.WriteFile(pathGotSVG, svgBytes, 0600)
t.Fatal(err)
}
err = diff.TestdataJSON(filepath.Join(dataPath, "board"), diagram)
assert.Success(t, err)
if os.Getenv("SKIP_SVG_CHECK") == "" {
err = xdiff.TestdataGeneric(filepath.Join(dataPath, "sketch"), ".svg", svgBytes)
if err != nil {
ioutil.WriteFile(pathGotSVG, svgBytes, 0600)
t.Fatal(err)
err = diff.Testdata(filepath.Join(dataPath, "sketch"), ".svg", svgBytes)
assert.Success(t, err)
}
}
err = ioutil.WriteFile(pathExpSVG, svgBytes, 0600)
if err != nil {
t.Fatal(err)
}
os.Remove(filepath.Join(dataPath, "sketch.got.svg"))
}
}
func getShape(t *testing.T, diagram *d2target.Diagram, id string) d2target.Shape {

2
go.mod generated
View file

@ -25,7 +25,7 @@ require (
nhooyr.io/websocket v1.8.7
oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541
oss.terrastruct.com/util-go v0.0.0-20221201174206-77c68780d6f0
oss.terrastruct.com/util-go v0.0.0-20221201180807-58edfec77805
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f
oss.terrastruct.com/xdefer v0.0.0-20221017222355-6f3b6e4d1557
oss.terrastruct.com/xjson v0.0.0-20221018000420-4986731c4c4a

4
go.sum generated
View file

@ -802,8 +802,8 @@ oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c h1:C1DDLzj2NrVi1YJ
oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c/go.mod h1:C8u/lYTvQWc1xC7rHpgFfpScfQC4NMeGGMmlKVZZUXM=
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541 h1:I9B1O1IJ6spivIQxbFRZmbhAwVeLwrcQRR1JbYUOvrI=
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541/go.mod h1:ags2QDy/T6jr69hT6bpmAmhr2H98n9o8Atf3QlUJPiU=
oss.terrastruct.com/util-go v0.0.0-20221201174206-77c68780d6f0 h1:zaSiaG+BkHysYOs7M/flh1FEOlJzqA+wwQvCplMBhxA=
oss.terrastruct.com/util-go v0.0.0-20221201174206-77c68780d6f0/go.mod h1:AN5T0bJ89/Q6ZebXIdPGpwAqVhK9PkDpgygWJaT2JzQ=
oss.terrastruct.com/util-go v0.0.0-20221201180807-58edfec77805 h1:DaMNXVEa+ZapP2U2gBVehedPdFsT7GGsGM+d7erCGAY=
oss.terrastruct.com/util-go v0.0.0-20221201180807-58edfec77805/go.mod h1:AN5T0bJ89/Q6ZebXIdPGpwAqVhK9PkDpgygWJaT2JzQ=
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f h1:7voRCwKM7TZkTo9u7hj+uV/zXoVB8czWrTq6MVIh3dg=
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f/go.mod h1:Y0coTLsWwX0q3a+/Ndq797t+vWyxm42T49Ik3bzaDKY=
oss.terrastruct.com/xdefer v0.0.0-20221017222355-6f3b6e4d1557 h1:rPbhJbN1q7B4tnppSPoAMwq0t6Pk5SrQDQ5S6uoNNHg=

View file

@ -1,38 +0,0 @@
package diff
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"oss.terrastruct.com/diff"
)
// TODO refactor with diff repo
func TestdataGeneric(path, fileExtension string, got []byte) (err error) {
expPath := fmt.Sprintf("%s.exp%s", path, fileExtension)
gotPath := fmt.Sprintf("%s.got%s", path, fileExtension)
err = os.MkdirAll(filepath.Dir(gotPath), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(gotPath, got, 0600)
if err != nil {
return err
}
ds, err := diff.Files(expPath, gotPath)
if err != nil {
return err
}
if ds != "" {
if os.Getenv("TESTDATA_ACCEPT") != "" {
return os.Rename(gotPath, expPath)
}
return fmt.Errorf("diff (rerun with $TESTDATA_ACCEPT=1 to accept):\n%s", ds)
}
return os.Remove(gotPath)
}