Skip to content

Commit

Permalink
refactor: simplify tests for api.Generate (#3031)
Browse files Browse the repository at this point in the history
* refactor: simplify tests for Generate

* Add deleted files to git ignore

Signed-off-by: Steve Coffman <steve@khanacademy.org>

---------

Signed-off-by: Steve Coffman <steve@khanacademy.org>
Co-authored-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
alexandear and StevenACoffman authored May 1, 2024
1 parent 28405ac commit 9e8e7ed
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8,205 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ gqlgen
*.exe

node_modules

# generated files
/api/testdata/default/graph/generated.go
/api/testdata/federation2/graph/federation.go
/api/testdata/federation2/graph/generated.go
53 changes: 23 additions & 30 deletions api/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,52 @@ package api

import (
"os"
"path"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/99designs/gqlgen/codegen/config"
)

func cleanup(workDir string) {
_ = os.Remove(path.Join(workDir, "server.go"))
_ = os.RemoveAll(path.Join(workDir, "graph", "generated"))
_ = os.Remove(path.Join(workDir, "graph", "resolver.go"))
_ = os.Remove(path.Join(workDir, "graph", "schema.resolvers.go"))
_ = os.Remove(path.Join(workDir, "graph", "model", "models_gen.go"))
_ = os.Remove(filepath.Join(workDir, "server.go"))
_ = os.Remove(filepath.Join(workDir, "graph", "generated.go"))
_ = os.Remove(filepath.Join(workDir, "graph", "resolver.go"))
_ = os.Remove(filepath.Join(workDir, "graph", "federation.go"))
_ = os.Remove(filepath.Join(workDir, "graph", "schema.resolvers.go"))
_ = os.Remove(filepath.Join(workDir, "graph", "model", "models_gen.go"))
}

func TestGenerate(t *testing.T) {
wd, _ := os.Getwd()
type args struct {
workDir string
}
wd, err := os.Getwd()
require.NoError(t, err)
tests := []struct {
name string
args args
wantErr bool
workDir string
}{
{
name: "default",
args: args{
workDir: path.Join(wd, "testdata", "default"),
},
wantErr: false,
name: "default",
workDir: filepath.Join(wd, "testdata", "default"),
},
{
name: "federation2",
args: args{
workDir: path.Join(wd, "testdata", "federation2"),
},
wantErr: false,
name: "federation2",
workDir: filepath.Join(wd, "testdata", "federation2"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
cleanup(tt.args.workDir)
t.Cleanup(func() {
cleanup(tt.workDir)
_ = os.Chdir(wd)
}()
_ = os.Chdir(tt.args.workDir)
})
err = os.Chdir(tt.workDir)
require.NoError(t, err)
cfg, err := config.LoadConfigFromDefaultLocations()
require.Nil(t, err, "failed to load config")
if err := Generate(cfg); (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
require.NoError(t, err, "failed to load config")
err = Generate(cfg)
assert.NoError(t, err, "failed to generate code")
})
}
}
Loading

0 comments on commit 9e8e7ed

Please sign in to comment.