|
| 1 | +// This source file is part of the EdgeDB open source project. |
| 2 | +// |
| 3 | +// Copyright EdgeDB Inc. and the EdgeDB authors. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/fs" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + "os/exec" |
| 26 | + "path/filepath" |
| 27 | + "strings" |
| 28 | + "sync" |
| 29 | + "testing" |
| 30 | + |
| 31 | + edgedb "github.com/edgedb/edgedb-go/internal/client" |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | +) |
| 35 | + |
| 36 | +var dsn string |
| 37 | + |
| 38 | +func TestMain(m *testing.M) { |
| 39 | + o := edgedb.TestClientOptions() |
| 40 | + pwd, ok := o.Password.Get() |
| 41 | + if !ok { |
| 42 | + log.Fatal("missing password") |
| 43 | + } |
| 44 | + dsn = fmt.Sprintf( |
| 45 | + "edgedb://%s:%s@%s:%d?tls_security=%s&tls_ca_file=%s", |
| 46 | + o.User, |
| 47 | + pwd, |
| 48 | + o.Host, |
| 49 | + o.Port, |
| 50 | + o.TLSOptions.SecurityMode, |
| 51 | + o.TLSOptions.CAFile, |
| 52 | + ) |
| 53 | + os.Exit(m.Run()) |
| 54 | +} |
| 55 | + |
| 56 | +func TestEdgeQLGo(t *testing.T) { |
| 57 | + dir, err := os.MkdirTemp("", "edgeql-go-*") |
| 58 | + require.NoError(t, err) |
| 59 | + defer func() { |
| 60 | + assert.NoError(t, os.RemoveAll(dir)) |
| 61 | + }() |
| 62 | + |
| 63 | + t.Log("building edgeql-go") |
| 64 | + edgeqlGo := filepath.Join(dir, "edgeql-go") |
| 65 | + run(t, ".", "go", "build", "-o", edgeqlGo) |
| 66 | + |
| 67 | + var wg sync.WaitGroup |
| 68 | + err = filepath.WalkDir( |
| 69 | + "testdata", |
| 70 | + func(src string, d fs.DirEntry, e error) error { |
| 71 | + require.NoError(t, e) |
| 72 | + if src == "testdata" { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + dst := filepath.Join(dir, strings.TrimPrefix(src, "testdata")) |
| 77 | + if d.IsDir() { |
| 78 | + e = os.Mkdir(dst, os.ModePerm) |
| 79 | + require.NoError(t, e) |
| 80 | + } else { |
| 81 | + wg.Add(1) |
| 82 | + go func() { |
| 83 | + defer wg.Done() |
| 84 | + copyFile(t, dst, src) |
| 85 | + }() |
| 86 | + } |
| 87 | + return nil |
| 88 | + }, |
| 89 | + ) |
| 90 | + require.NoError(t, err) |
| 91 | + wg.Wait() |
| 92 | + |
| 93 | + entries, err := os.ReadDir(dir) |
| 94 | + require.NoError(t, err) |
| 95 | + for _, entry := range entries { |
| 96 | + if entry.Name() == "edgeql-go" { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + t.Run(entry.Name(), func(t *testing.T) { |
| 101 | + projectDir := filepath.Join(dir, entry.Name()) |
| 102 | + run(t, projectDir, edgeqlGo) |
| 103 | + run(t, projectDir, "go", "run", "./...") |
| 104 | + er := filepath.WalkDir( |
| 105 | + projectDir, |
| 106 | + func(f string, d fs.DirEntry, e error) error { |
| 107 | + require.NoError(t, e) |
| 108 | + if strings.HasSuffix(f, ".go.assert") { |
| 109 | + checkAssertFile(t, f) |
| 110 | + } |
| 111 | + if strings.HasSuffix(f, ".go") && |
| 112 | + !strings.HasSuffix(f, "ignore.go") { |
| 113 | + checkGoFile(t, f) |
| 114 | + } |
| 115 | + return nil |
| 116 | + }, |
| 117 | + ) |
| 118 | + require.NoError(t, er) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func checkAssertFile(t *testing.T, file string) { |
| 124 | + t.Helper() |
| 125 | + goFile := strings.TrimSuffix(file, ".assert") |
| 126 | + if assert.FileExistsf(t, goFile, "missing .go file for %s", file) { |
| 127 | + assertEqualFiles(t, file, goFile) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func checkGoFile(t *testing.T, file string) { |
| 132 | + t.Helper() |
| 133 | + assertFile := file + ".assert" |
| 134 | + if assert.FileExistsf(t, assertFile, |
| 135 | + "missing .go.assert file for %s", file, |
| 136 | + ) { |
| 137 | + assertEqualFiles(t, assertFile, file) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func assertEqualFiles(t *testing.T, left, right string) { |
| 142 | + t.Helper() |
| 143 | + leftData, err := os.ReadFile(left) |
| 144 | + require.NoErrorf(t, err, "reading %s", left) |
| 145 | + |
| 146 | + rightData, err := os.ReadFile(right) |
| 147 | + require.NoErrorf(t, err, "reading %s", right) |
| 148 | + |
| 149 | + assert.Equal(t, string(leftData), string(rightData), |
| 150 | + "files are not equal: %s != %s", left, right, |
| 151 | + ) |
| 152 | +} |
| 153 | + |
| 154 | +func copyFile(t *testing.T, to, from string) { |
| 155 | + toFd, err := os.Create(to) |
| 156 | + require.NoError(t, err) |
| 157 | + defer func() { |
| 158 | + assert.NoError(t, toFd.Close()) |
| 159 | + }() |
| 160 | + |
| 161 | + fromFd, err := os.Open(from) |
| 162 | + require.NoError(t, err) |
| 163 | + defer func() { |
| 164 | + assert.NoError(t, fromFd.Close()) |
| 165 | + }() |
| 166 | + |
| 167 | + _, err = io.Copy(toFd, fromFd) |
| 168 | + require.NoError(t, err) |
| 169 | +} |
| 170 | + |
| 171 | +func run(t *testing.T, dir, name string, args ...string) { |
| 172 | + cmd := exec.Command(name, args...) |
| 173 | + cmd.Dir = dir |
| 174 | + cmd.Stdout = os.Stdout |
| 175 | + cmd.Stderr = os.Stderr |
| 176 | + cmd.Env = append(os.Environ(), fmt.Sprintf("EDGEDB_DSN=%s", dsn)) |
| 177 | + require.NoError(t, cmd.Run()) |
| 178 | +} |
0 commit comments