forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabaseFetcher.test.ts
More file actions
163 lines (140 loc) · 5.1 KB
/
databaseFetcher.test.ts
File metadata and controls
163 lines (140 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'vscode-test';
import 'mocha';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as tmp from 'tmp';
import * as chai from 'chai';
import { window } from 'vscode';
import {
convertToDatabaseUrl,
looksLikeLgtmUrl,
findDirWithFile,
} from '../../databaseFetcher';
chai.use(chaiAsPromised);
const expect = chai.expect;
describe('databaseFetcher', function() {
// These tests make API calls and may need extra time to complete.
this.timeout(10000);
describe('convertToDatabaseUrl', () => {
let sandbox: sinon.SinonSandbox;
let quickPickSpy: sinon.SinonStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
quickPickSpy = sandbox.stub(window, 'showQuickPick');
});
afterEach(() => {
sandbox.restore();
});
it('should convert a project url to a database url', async () => {
quickPickSpy.resolves('javascript');
const lgtmUrl = 'https://lgtm.com/projects/g/github/codeql';
const dbUrl = await convertToDatabaseUrl(lgtmUrl);
expect(dbUrl).to.equal(
'https://lgtm.com/api/v1.0/snapshots/1506465042581/javascript'
);
expect(quickPickSpy.firstCall.args[0]).to.contain('javascript');
expect(quickPickSpy.firstCall.args[0]).to.contain('python');
});
it('should convert a project url to a database url with extra path segments', async () => {
quickPickSpy.resolves('python');
const lgtmUrl =
'https://lgtm.com/projects/g/github/codeql/subpage/subpage2?query=xxx';
const dbUrl = await convertToDatabaseUrl(lgtmUrl);
expect(dbUrl).to.equal(
'https://lgtm.com/api/v1.0/snapshots/1506465042581/python'
);
});
it('should convert a raw slug to a database url with extra path segments', async () => {
quickPickSpy.resolves('python');
const lgtmUrl =
'g/github/codeql';
const dbUrl = await convertToDatabaseUrl(lgtmUrl);
expect(dbUrl).to.equal(
'https://lgtm.com/api/v1.0/snapshots/1506465042581/python'
);
});
it('should fail on a nonexistant prohect', async () => {
quickPickSpy.resolves('javascript');
const lgtmUrl = 'https://lgtm.com/projects/g/github/hucairz';
expect(convertToDatabaseUrl(lgtmUrl)).to.rejectedWith(/Invalid LGTM URL/);
});
});
describe('looksLikeLgtmUrl', () => {
it('should handle invalid urls', () => {
expect(looksLikeLgtmUrl('')).to.be.false;
expect(looksLikeLgtmUrl('http://lgtm.com/projects/g/github/codeql')).to.be
.false;
expect(looksLikeLgtmUrl('https://ww.lgtm.com/projects/g/github/codeql'))
.to.be.false;
expect(looksLikeLgtmUrl('https://ww.lgtm.com/projects/g/github')).to.be
.false;
expect(looksLikeLgtmUrl('g/github')).to.be
.false;
expect(looksLikeLgtmUrl('ggg/github/myproj')).to.be
.false;
});
it('should handle valid urls', () => {
expect(looksLikeLgtmUrl('https://lgtm.com/projects/g/github/codeql')).to
.be.true;
expect(looksLikeLgtmUrl('https://www.lgtm.com/projects/g/github/codeql'))
.to.be.true;
expect(
looksLikeLgtmUrl('https://lgtm.com/projects/g/github/codeql/sub/pages')
).to.be.true;
expect(
looksLikeLgtmUrl(
'https://lgtm.com/projects/g/github/codeql/sub/pages?query=string'
)
).to.be.true;
expect(looksLikeLgtmUrl('g/github/myproj')).to.be
.true;
expect(looksLikeLgtmUrl('git/github/myproj')).to.be
.true;
});
});
describe('findDirWithFile', () => {
let dir: tmp.DirResult;
beforeEach(() => {
dir = tmp.dirSync({ unsafeCleanup: true });
createFile('a');
createFile('b');
createFile('c');
createDir('dir1');
createFile('dir1', 'd');
createFile('dir1', 'e');
createFile('dir1', 'f');
createDir('dir2');
createFile('dir2', 'g');
createFile('dir2', 'h');
createFile('dir2', 'i');
createDir('dir2', 'dir3');
createFile('dir2', 'dir3', 'j');
createFile('dir2', 'dir3', 'k');
createFile('dir2', 'dir3', 'l');
});
it('should find files', async () => {
expect(await findDirWithFile(dir.name, 'k')).to.equal(
path.join(dir.name, 'dir2', 'dir3')
);
expect(await findDirWithFile(dir.name, 'h')).to.equal(
path.join(dir.name, 'dir2')
);
expect(await findDirWithFile(dir.name, 'z', 'a')).to.equal(dir.name);
// there's some slight indeterminism when more than one name exists
// but in general, this will find files in the current directory before
// finding files in sub-dirs
expect(await findDirWithFile(dir.name, 'k', 'a')).to.equal(dir.name);
});
it('should not find files', async () => {
expect(await findDirWithFile(dir.name, 'x', 'y', 'z')).to.be.undefined;
});
function createFile(...segments: string[]) {
fs.createFileSync(path.join(dir.name, ...segments));
}
function createDir(...segments: string[]) {
fs.mkdirSync(path.join(dir.name, ...segments));
}
});
});