|
| 1 | +import { FileSchemaProvider } from "../file"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import { Debug } from "../../../utilities"; |
| 5 | +import { isDone } from "nock"; |
| 6 | + |
| 7 | +const makeNestedDir = dir => { |
| 8 | + if (fs.existsSync(dir)) return; |
| 9 | + |
| 10 | + try { |
| 11 | + fs.mkdirSync(dir); |
| 12 | + } catch (err) { |
| 13 | + if (err.code == "ENOENT") { |
| 14 | + makeNestedDir(path.dirname(dir)); //create parent dir |
| 15 | + makeNestedDir(dir); //create dir |
| 16 | + } |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +const deleteFolderRecursive = path => { |
| 21 | + // don't delete files on azure CI |
| 22 | + if (process.env.AZURE_HTTP_USER_AGENT) return; |
| 23 | + |
| 24 | + if (fs.existsSync(path)) { |
| 25 | + fs.readdirSync(path).forEach(function(file, index) { |
| 26 | + var curPath = path + "/" + file; |
| 27 | + if (fs.lstatSync(curPath).isDirectory()) { |
| 28 | + // recurse |
| 29 | + deleteFolderRecursive(curPath); |
| 30 | + } else { |
| 31 | + // delete file |
| 32 | + fs.unlinkSync(curPath); |
| 33 | + } |
| 34 | + }); |
| 35 | + fs.rmdirSync(path); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const writeFilesToDir = (dir: string, files: Record<string, string>) => { |
| 40 | + Object.keys(files).forEach(key => { |
| 41 | + if (key.includes("/")) makeNestedDir(path.dirname(key)); |
| 42 | + fs.writeFileSync(`${dir}/${key}`, files[key]); |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +describe("FileSchemaProvider", () => { |
| 47 | + let dir, dirPath; |
| 48 | + |
| 49 | + // set up a temp dir |
| 50 | + beforeEach(() => { |
| 51 | + dir = fs.mkdtempSync("__tmp__"); |
| 52 | + dirPath = `${process.cwd()}/${dir}`; |
| 53 | + }); |
| 54 | + |
| 55 | + // clean up our temp dir |
| 56 | + afterEach(() => { |
| 57 | + if (dir) deleteFolderRecursive(dir); |
| 58 | + dir = dirPath = undefined; |
| 59 | + }); |
| 60 | + |
| 61 | + it("finds and loads sdl from graphql file", async () => { |
| 62 | + const writtenSDL = ` |
| 63 | + extend type Query { |
| 64 | + myProduct: Product |
| 65 | + } |
| 66 | +
|
| 67 | + type Product @key(fields: "id") { |
| 68 | + id: ID |
| 69 | + sku: ID |
| 70 | + name: string |
| 71 | + } |
| 72 | + `; |
| 73 | + writeFilesToDir(dir, { |
| 74 | + "schema.graphql": writtenSDL |
| 75 | + }); |
| 76 | + |
| 77 | + const provider = new FileSchemaProvider({ path: dir + "/schema.graphql" }); |
| 78 | + const sdl = await provider.resolveFederatedServiceSDL(); |
| 79 | + expect(sdl).toEqual(writtenSDL); |
| 80 | + }); |
| 81 | + |
| 82 | + it("errors when sdl file is not a graphql file", async () => { |
| 83 | + const toWrite = ` |
| 84 | + module.exports = \` |
| 85 | + extend type Query { |
| 86 | + myProduct: Product |
| 87 | + } |
| 88 | +
|
| 89 | + type Product @key(fields: "id") { |
| 90 | + id: ID |
| 91 | + sku: ID |
| 92 | + name: string |
| 93 | + }\` |
| 94 | + `; |
| 95 | + writeFilesToDir(dir, { |
| 96 | + "schema.js": toWrite |
| 97 | + }); |
| 98 | + |
| 99 | + const errorSpy = jest.spyOn(Debug, "error"); |
| 100 | + |
| 101 | + // noop -- just silence the error |
| 102 | + errorSpy.mockImplementationOnce(() => {}); |
| 103 | + |
| 104 | + const provider = new FileSchemaProvider({ path: dir + "/schema.js" }); |
| 105 | + const sdl = await provider.resolveFederatedServiceSDL(); |
| 106 | + expect(errorSpy).toBeCalledTimes(1); |
| 107 | + }); |
| 108 | +}); |
0 commit comments