|
| 1 | +import { join } from "path"; |
| 2 | +import { dirSync } from "tmp-promise"; |
| 3 | +import { DirResult } from "tmp"; |
| 4 | +import { outputFile } from "fs-extra"; |
| 5 | +import { dump } from "js-yaml"; |
| 6 | +import { QueryLanguage } from "../../../src/common/query-language"; |
| 7 | +import { getQlPackLanguage } from "../../../src/common/qlpack-language"; |
| 8 | + |
| 9 | +describe("getQlPackLanguage", () => { |
| 10 | + let tmpDir: DirResult; |
| 11 | + let qlpackPath: string; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + tmpDir = dirSync({ |
| 15 | + prefix: "queries_", |
| 16 | + keep: false, |
| 17 | + unsafeCleanup: true, |
| 18 | + }); |
| 19 | + |
| 20 | + qlpackPath = join(tmpDir.name, "qlpack.yml"); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + tmpDir.removeCallback(); |
| 25 | + }); |
| 26 | + |
| 27 | + it.each(Object.values(QueryLanguage))( |
| 28 | + "should find a single language %s", |
| 29 | + async (language) => { |
| 30 | + await writeYAML(qlpackPath, { |
| 31 | + name: "test", |
| 32 | + dependencies: { |
| 33 | + [`codeql/${language}-all`]: "^0.7.0", |
| 34 | + "my-custom-pack/test": "${workspace}", |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + const result = await getQlPackLanguage(qlpackPath); |
| 39 | + expect(result).toEqual(language); |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + it("should find nothing when there is no dependencies key", async () => { |
| 44 | + await writeYAML(qlpackPath, { |
| 45 | + name: "test", |
| 46 | + }); |
| 47 | + |
| 48 | + const result = await getQlPackLanguage(qlpackPath); |
| 49 | + expect(result).toEqual(undefined); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should find nothing when the dependencies are empty", async () => { |
| 53 | + await writeYAML(qlpackPath, { |
| 54 | + name: "test", |
| 55 | + dependencies: {}, |
| 56 | + }); |
| 57 | + |
| 58 | + const result = await getQlPackLanguage(qlpackPath); |
| 59 | + expect(result).toEqual(undefined); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should find nothing when dependencies is a scalar", async () => { |
| 63 | + await writeYAML(qlpackPath, { |
| 64 | + name: "test", |
| 65 | + dependencies: "codeql/java-all", |
| 66 | + }); |
| 67 | + |
| 68 | + const result = await getQlPackLanguage(qlpackPath); |
| 69 | + expect(result).toEqual(undefined); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should find nothing when dependencies is an array", async () => { |
| 73 | + await writeYAML(qlpackPath, { |
| 74 | + name: "test", |
| 75 | + dependencies: ["codeql/java-all"], |
| 76 | + }); |
| 77 | + |
| 78 | + const result = await getQlPackLanguage(qlpackPath); |
| 79 | + expect(result).toEqual(undefined); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should find nothing when there are no matching dependencies", async () => { |
| 83 | + await writeYAML(qlpackPath, { |
| 84 | + name: "test", |
| 85 | + dependencies: { |
| 86 | + "codeql/java-queries": "*", |
| 87 | + "github/my-test-query-pack": "*", |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + const result = await getQlPackLanguage(qlpackPath); |
| 92 | + expect(result).toEqual(undefined); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should find nothing when there are multiple matching dependencies", async () => { |
| 96 | + await writeYAML(qlpackPath, { |
| 97 | + name: "test", |
| 98 | + dependencies: { |
| 99 | + "codeql/java-all": "*", |
| 100 | + "codeql/csharp-all": "*", |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + const result = await getQlPackLanguage(qlpackPath); |
| 105 | + expect(result).toEqual(undefined); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should throw when the file does not exist", async () => { |
| 109 | + await expect(getQlPackLanguage(qlpackPath)).rejects.toBeDefined(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should throw when reading a directory", async () => { |
| 113 | + await expect(getQlPackLanguage(tmpDir.name)).rejects.toBeDefined(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should throw when the file is invalid YAML", async () => { |
| 117 | + await outputFile(qlpackPath, `name: test\n foo: bar`); |
| 118 | + |
| 119 | + await expect(getQlPackLanguage(tmpDir.name)).rejects.toBeDefined(); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +async function writeYAML(path: string, yaml: unknown): Promise<void> { |
| 124 | + await outputFile(path, dump(yaml), "utf-8"); |
| 125 | +} |
0 commit comments