|
1 | 1 | package dinosql
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path" |
4 | 7 | "testing"
|
5 | 8 |
|
6 | 9 | "github.com/google/go-cmp/cmp"
|
@@ -184,3 +187,59 @@ func TestExpand(t *testing.T) {
|
184 | 187 | t.Errorf("mismatch:\nexpected: %s\n acutal: %s", expected, actual)
|
185 | 188 | }
|
186 | 189 | }
|
| 190 | + |
| 191 | +func TestReadFiles(t *testing.T) { |
| 192 | + dir, err := ioutil.TempDir("", "sqlc-test-read-files") |
| 193 | + if err != nil { |
| 194 | + t.Error(err) |
| 195 | + } |
| 196 | + defer os.RemoveAll(dir) |
| 197 | + |
| 198 | + subdir1 := path.Join(dir, "subdir1") |
| 199 | + if err := os.Mkdir(subdir1, 0777); err != nil { |
| 200 | + t.Error(err) |
| 201 | + } |
| 202 | + |
| 203 | + subdir2 := path.Join(dir, "subdir2") |
| 204 | + if err := os.Mkdir(subdir2, 0777); err != nil { |
| 205 | + t.Error(err) |
| 206 | + } |
| 207 | + |
| 208 | + files := []string{ |
| 209 | + path.Join(subdir1, "include-me.sql"), |
| 210 | + path.Join(subdir1, "include-me.up.sql"), |
| 211 | + path.Join(subdir1, "not-me.down.sql"), |
| 212 | + path.Join(subdir2, "include-me.sql"), |
| 213 | + path.Join(subdir2, "include-me.up.sql"), |
| 214 | + path.Join(subdir2, "not-me.down.sql"), |
| 215 | + } |
| 216 | + for _, filename := range files { |
| 217 | + fd, err := os.Create(filename) |
| 218 | + if err != nil { |
| 219 | + t.Error(err) |
| 220 | + } |
| 221 | + defer fd.Close() |
| 222 | + } |
| 223 | + |
| 224 | + input := []string{ |
| 225 | + subdir1, |
| 226 | + path.Join(subdir2, "include-me.sql"), |
| 227 | + path.Join(subdir2, "include-me.up.sql"), |
| 228 | + path.Join(subdir2, "not-me.down.sql"), |
| 229 | + } |
| 230 | + |
| 231 | + expectedFiles := []string{ |
| 232 | + path.Join(subdir1, "include-me.sql"), |
| 233 | + path.Join(subdir1, "include-me.up.sql"), |
| 234 | + path.Join(subdir2, "include-me.sql"), |
| 235 | + path.Join(subdir2, "include-me.up.sql"), |
| 236 | + } |
| 237 | + |
| 238 | + filesRead, err := ReadSQLFiles(input) |
| 239 | + if err != nil { |
| 240 | + t.Error(err) |
| 241 | + } |
| 242 | + if !cmp.Equal(expectedFiles, filesRead) { |
| 243 | + t.Errorf("unexpected files: %s", cmp.Diff(expectedFiles, filesRead)) |
| 244 | + } |
| 245 | +} |
0 commit comments