Skip to content

Commit a3bbdaf

Browse files
committed
Add tests for 'tryGetRemoteLocation'
1 parent a78eef4 commit a3bbdaf

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

extensions/ql-vscode/test/pure-tests/location.test.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
22
import 'mocha';
3-
import { tryGetResolvableLocation } from '../../src/pure/bqrs-utils';
3+
import { tryGetRemoteLocation, tryGetResolvableLocation } from '../../src/pure/bqrs-utils';
44

55
describe('processing string locations', function() {
66
it('should detect Windows whole-file locations', function() {
@@ -32,3 +32,79 @@ describe('processing string locations', function() {
3232
}
3333
});
3434
});
35+
36+
describe('getting links to remote (GitHub) locations', function() {
37+
it('should return undefined if resolvableLocation is undefined', function() {
38+
const loc = 'not a location';
39+
const fileLinkPrefix = '';
40+
const sourceLocationPrefix = '';
41+
42+
const link = tryGetRemoteLocation(loc, fileLinkPrefix, sourceLocationPrefix);
43+
44+
expect(link).to.be.undefined;
45+
});
46+
47+
it('should return undefined if resolvableLocation has the wrong format', function() {
48+
const loc = {
49+
uri: 'file:/path/to/file.ext',
50+
startLine: 194,
51+
startColumn: 18,
52+
endLine: 237,
53+
endColumn: 1,
54+
};
55+
const fileLinkPrefix = '';
56+
const sourceLocationPrefix = '/home/foo/bar';
57+
58+
const link = tryGetRemoteLocation(loc, fileLinkPrefix, sourceLocationPrefix);
59+
60+
expect(link).to.be.undefined;
61+
});
62+
63+
it('should return a remote file ref if the sourceLocationPrefix and resolvableLocation match up', function() {
64+
const loc = {
65+
uri: 'file:/home/foo/bar/path/to/file.ext',
66+
startLine: 194,
67+
startColumn: 18,
68+
endLine: 237,
69+
endColumn: 1,
70+
};
71+
const fileLinkPrefix = 'https://github.com/owner/repo/blob/sha1234';
72+
const sourceLocationPrefix = '/home/foo/bar';
73+
74+
const link = tryGetRemoteLocation(loc, fileLinkPrefix, sourceLocationPrefix);
75+
76+
expect(link).to.eql('https://github.com/owner/repo/blob/sha1234/path/to/file.ext#L194-L237');
77+
});
78+
79+
it('should return undefined if the sourceLocationPrefix is missing and resolvableLocation doesn\'t match the default format', function() {
80+
const loc = {
81+
uri: 'file:/home/foo/bar/path/to/file.ext',
82+
startLine: 194,
83+
startColumn: 18,
84+
endLine: 237,
85+
endColumn: 1,
86+
};
87+
const fileLinkPrefix = 'https://github.com/owner/repo/blob/sha1234';
88+
const sourceLocationPrefix = '';
89+
90+
const link = tryGetRemoteLocation(loc, fileLinkPrefix, sourceLocationPrefix);
91+
92+
expect(link).to.eql(undefined);
93+
});
94+
95+
it('should return a remote file ref if the sourceLocationPrefix is missing, but the resolvableLocation matches the default format', function() {
96+
const loc = {
97+
uri: 'file:/home/runner/work/foo/bar/path/to/file.ext',
98+
startLine: 194,
99+
startColumn: 18,
100+
endLine: 237,
101+
endColumn: 1,
102+
};
103+
const fileLinkPrefix = 'https://github.com/owner/repo/blob/sha1234';
104+
const sourceLocationPrefix = '';
105+
106+
const link = tryGetRemoteLocation(loc, fileLinkPrefix, sourceLocationPrefix);
107+
108+
expect(link).to.eql('https://github.com/owner/repo/blob/sha1234/path/to/file.ext#L194-L237');
109+
});
110+
});

0 commit comments

Comments
 (0)