Skip to content

Commit a338683

Browse files
committed
Add unit tests for databaseFetcher
1 parent a541b11 commit a338683

File tree

2 files changed

+147
-2
lines changed

2 files changed

+147
-2
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
withProgress,
1717
showAndLogInformationMessage,
1818
} from "./helpers";
19+
import { logger } from "./logging";
1920

2021
/**
2122
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
@@ -305,7 +306,8 @@ function isFile(databaseUrl: string) {
305306
*
306307
* @returns the directory containing the file, or undefined if not found.
307308
*/
308-
async function findDirWithFile(
309+
// exported for testing
310+
export async function findDirWithFile(
309311
dir: string,
310312
...toFind: string[]
311313
): Promise<string | undefined> {
@@ -340,6 +342,8 @@ async function findDirWithFile(
340342
*
341343
* @return true if this looks like an LGTM project url
342344
*/
345+
// exported for testing
346+
export function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string {
343347
if (!lgtmUrl) {
344348
return false;
345349
}
@@ -361,7 +365,8 @@ async function findDirWithFile(
361365
}
362366
}
363367

364-
async function convertToDatabaseUrl(lgtmUrl: string) {
368+
// exported for testing
369+
export async function convertToDatabaseUrl(lgtmUrl: string) {
365370
try {
366371
const uri = Uri.parse(lgtmUrl, true);
367372
const paths = ["api", "v1.0"].concat(
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import "vscode-test";
2+
import "mocha";
3+
import * as chaiAsPromised from "chai-as-promised";
4+
import * as sinon from "sinon";
5+
// import * as sinonChai from 'sinon-chai';
6+
import * as path from "path";
7+
import * as fs from "fs-extra";
8+
import * as tmp from "tmp";
9+
import * as chai from "chai";
10+
import { window } from "vscode";
11+
12+
import {
13+
convertToDatabaseUrl,
14+
looksLikeLgtmUrl,
15+
findDirWithFile,
16+
} from "../../databaseFetcher";
17+
chai.use(chaiAsPromised);
18+
const expect = chai.expect;
19+
20+
describe("databaseFetcher", () => {
21+
describe("convertToDatabaseUrl", () => {
22+
let quickPickSpy: sinon.SinonStub;
23+
beforeEach(() => {
24+
quickPickSpy = sinon.stub(window, "showQuickPick");
25+
});
26+
27+
afterEach(() => {
28+
(window.showQuickPick as sinon.SinonStub).restore();
29+
});
30+
31+
it("should convert a project url to a database url", async () => {
32+
quickPickSpy.returns("javascript" as any);
33+
const lgtmUrl = "https://lgtm.com/projects/g/github/codeql";
34+
const dbUrl = await convertToDatabaseUrl(lgtmUrl);
35+
36+
expect(dbUrl).to.equal(
37+
"https://lgtm.com/api/v1.0/snapshots/1506465042581/javascript"
38+
);
39+
expect(quickPickSpy.firstCall.args[0]).to.contain("javascript");
40+
expect(quickPickSpy.firstCall.args[0]).to.contain("python");
41+
});
42+
43+
it("should convert a project url to a database url with extra path segments", async () => {
44+
quickPickSpy.returns("python" as any);
45+
const lgtmUrl =
46+
"https://lgtm.com/projects/g/github/codeql/subpage/subpage2?query=xxx";
47+
const dbUrl = await convertToDatabaseUrl(lgtmUrl);
48+
49+
expect(dbUrl).to.equal(
50+
"https://lgtm.com/api/v1.0/snapshots/1506465042581/python"
51+
);
52+
});
53+
54+
it("should fail on a nonexistant prohect", async () => {
55+
quickPickSpy.returns("javascript" as any);
56+
const lgtmUrl = "https://lgtm.com/projects/g/github/hucairz";
57+
expect(convertToDatabaseUrl(lgtmUrl)).to.rejectedWith(/Invalid LGTM URL/);
58+
});
59+
});
60+
61+
describe("looksLikeLgtmUrl", () => {
62+
it("should handle invalid urls", () => {
63+
expect(looksLikeLgtmUrl("")).to.be.false;
64+
expect(looksLikeLgtmUrl("http://lgtm.com/projects/g/github/codeql")).to.be
65+
.false;
66+
expect(looksLikeLgtmUrl("https://ww.lgtm.com/projects/g/github/codeql"))
67+
.to.be.false;
68+
expect(looksLikeLgtmUrl("https://ww.lgtm.com/projects/g/github")).to.be
69+
.false;
70+
});
71+
72+
it("should handle valid urls", () => {
73+
expect(looksLikeLgtmUrl("https://lgtm.com/projects/g/github/codeql")).to
74+
.be.true;
75+
expect(looksLikeLgtmUrl("https://www.lgtm.com/projects/g/github/codeql"))
76+
.to.be.true;
77+
expect(
78+
looksLikeLgtmUrl("https://lgtm.com/projects/g/github/codeql/sub/pages")
79+
).to.be.true;
80+
expect(
81+
looksLikeLgtmUrl(
82+
"https://lgtm.com/projects/g/github/codeql/sub/pages?query=string"
83+
)
84+
).to.be.true;
85+
});
86+
});
87+
88+
describe("findDirWithFile", () => {
89+
let dir: tmp.DirResult;
90+
beforeEach(() => {
91+
dir = tmp.dirSync({ unsafeCleanup: true });
92+
createFile("a");
93+
createFile("b");
94+
createFile("c");
95+
96+
createDir("dir1");
97+
createFile("dir1", "d");
98+
createFile("dir1", "e");
99+
createFile("dir1", "f");
100+
101+
createDir("dir2");
102+
createFile("dir2", "g");
103+
createFile("dir2", "h");
104+
createFile("dir2", "i");
105+
106+
createDir("dir2", "dir3");
107+
createFile("dir2", "dir3", "j");
108+
createFile("dir2", "dir3", "k");
109+
createFile("dir2", "dir3", "l");
110+
});
111+
112+
it("should find files", async () => {
113+
expect(await findDirWithFile(dir.name, "k")).to.equal(
114+
path.join(dir.name, "dir2", "dir3")
115+
);
116+
expect(await findDirWithFile(dir.name, "h")).to.equal(
117+
path.join(dir.name, "dir2")
118+
);
119+
expect(await findDirWithFile(dir.name, "z", "a")).to.equal(dir.name);
120+
// there's some slight indeterminism when more than one name exists
121+
// but in general, this will find files in the current directory before
122+
// finding files in sub-dirs
123+
expect(await findDirWithFile(dir.name, "k", "a")).to.equal(dir.name);
124+
});
125+
126+
127+
it("should not find files", async () => {
128+
expect(await findDirWithFile(dir.name, "x", "y", "z")).to.be.undefined;
129+
});
130+
131+
132+
function createFile(...segments: string[]) {
133+
fs.createFileSync(path.join(dir.name, ...segments));
134+
}
135+
136+
function createDir(...segments: string[]) {
137+
fs.mkdirSync(path.join(dir.name, ...segments));
138+
}
139+
});
140+
});

0 commit comments

Comments
 (0)