Skip to content

Commit f5f0d14

Browse files
refactor: Move test flags from package.json into TypeScript code (#102)
Co-authored-by: Valery Bugakov <skymk1@gmail.com>
1 parent fb9c64e commit f5f0d14

File tree

6 files changed

+221
-177
lines changed

6 files changed

+221
-177
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable @typescript-eslint/ban-ts-comment */
3+
// @ts-nocheck
4+
5+
// Stash the base directory into a global variable.
6+
global.__rootDirectory = __dirname + '/dist/';
7+
8+
require('./dist/scip-python-test');
9+
10+
// Q: Why do we have this stub file instead of directly
11+
// invoking a test running on `./test/test-main.ts`
12+
// or invoking `node ./dist/scip-python-test.ts`?
13+
//
14+
// A: There is some reliance on specific relative directory
15+
// structure in Pyright code, which means that if the
16+
// script is in a subdirectory, it cannot find type stubs
17+
// for stdlib modules, causing snapshot mismatches.

packages/pyright-scip/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"clean": "shx rm -rf ./dist ./out README.md LICENSE.txt",
99
"prepack": "npm run clean && shx cp ../../README.md . && shx cp ../../LICENSE.txt . && npm run build",
1010
"check-snapshots": "npm run update-snapshots -- --check",
11-
"update-snapshots": "node --enable-source-maps ./index.js snapshot-dir snapshots --environment snapshots/testEnv.json --quiet",
11+
"update-snapshots": "node --enable-source-maps ./index-test.js",
1212
"test": "jest --forceExit --detectOpenHandles",
1313
"webpack": "webpack --mode development --progress",
1414
"watch": "webpack --mode development --progress --watch"
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import * as child_process from 'child_process';
4+
5+
import { scip } from './scip';
6+
import { diffSnapshot, formatSnapshot, writeSnapshot } from './lib';
7+
import { Input } from './lsif-typescript/Input';
8+
import { join } from 'path';
9+
import { IndexOptions, SnapshotOptions, mainCommand } from './MainCommand';
10+
import { sendStatus, setQuiet, setShowProgressRateLimit } from './status';
11+
import { Indexer } from './indexer';
12+
import { exit } from 'process';
13+
14+
function indexAction(options: IndexOptions): void {
15+
setQuiet(options.quiet);
16+
if (options.showProgressRateLimit !== undefined) {
17+
setShowProgressRateLimit(options.showProgressRateLimit);
18+
}
19+
20+
const workspaceRoot = options.cwd;
21+
const snapshotDir = options.snapshotDir;
22+
const environment = options.environment;
23+
24+
const projectRoot = workspaceRoot;
25+
process.chdir(workspaceRoot);
26+
27+
// TODO: use setup.py / poetry to determine better projectName
28+
const projectName = options.projectName;
29+
if (!projectName || projectName == '') {
30+
console.warn('Must pass `--project-name`');
31+
return;
32+
}
33+
34+
// TODO: Use setup.py / poetry to determine better projectVersion
35+
// for now, the current hash works OK
36+
let projectVersion = options.projectVersion;
37+
if (!projectVersion || projectVersion === '') {
38+
// Default to current git hash
39+
try {
40+
projectVersion = child_process.execSync('git rev-parse HEAD').toString().trim();
41+
} catch (e) {
42+
console.warn('Must either pass `--project-version` or run from within a git repository');
43+
return;
44+
}
45+
}
46+
47+
const outputFile = path.join(projectRoot, options.output);
48+
const output = fs.openSync(outputFile, 'w');
49+
50+
sendStatus(`Indexing ${projectRoot} with version ${projectVersion}`);
51+
52+
try {
53+
let indexer = new Indexer({
54+
...options,
55+
workspaceRoot,
56+
projectRoot,
57+
projectName,
58+
projectVersion,
59+
environment,
60+
writeIndex: (partialIndex: scip.Index): void => {
61+
fs.writeSync(output, partialIndex.serializeBinary());
62+
},
63+
});
64+
65+
indexer.index();
66+
} catch (e) {
67+
console.warn(
68+
'\n\nExperienced Fatal Error While Indexing:\nPlease create an issue at github.com/sourcegraph/scip-python:',
69+
e
70+
);
71+
exit(1);
72+
}
73+
74+
fs.close(output);
75+
76+
if (snapshotDir) {
77+
sendStatus(`Writing snapshot from index: ${outputFile}`);
78+
79+
const scipIndex = scip.Index.deserializeBinary(fs.readFileSync(outputFile));
80+
for (const doc of scipIndex.documents) {
81+
if (doc.relative_path.startsWith('..')) {
82+
console.log('Skipping Doc:', doc.relative_path);
83+
continue;
84+
}
85+
86+
const inputPath = path.join(projectRoot, doc.relative_path);
87+
const input = Input.fromFile(inputPath);
88+
const obtained = formatSnapshot(input, doc, scipIndex.external_symbols);
89+
const relativeToInputDirectory = path.relative(projectRoot, inputPath);
90+
const outputPath = path.resolve(snapshotDir, relativeToInputDirectory);
91+
writeSnapshot(outputPath, obtained);
92+
}
93+
}
94+
}
95+
96+
function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
97+
setQuiet(options.quiet);
98+
if (options.showProgressRateLimit !== undefined) {
99+
setShowProgressRateLimit(options.showProgressRateLimit);
100+
}
101+
102+
console.log('... Snapshotting ... ');
103+
const projectName = options.projectName;
104+
const projectVersion = options.projectVersion;
105+
const environment = options.environment ? path.resolve(options.environment) : undefined;
106+
107+
const snapshotOnly = options.only;
108+
109+
const inputDirectory = path.resolve(join(snapshotRoot, 'input'));
110+
const outputDirectory = path.resolve(join(snapshotRoot, 'output'));
111+
112+
// Either read all the directories or just the one passed in by name
113+
let snapshotDirectories = fs.readdirSync(inputDirectory);
114+
if (snapshotOnly) {
115+
snapshotDirectories = [snapshotOnly];
116+
}
117+
118+
for (const snapshotDir of snapshotDirectories) {
119+
let projectRoot = join(inputDirectory, snapshotDir);
120+
if (!fs.lstatSync(projectRoot).isDirectory()) {
121+
continue;
122+
}
123+
124+
projectRoot = path.resolve(projectRoot);
125+
process.chdir(projectRoot);
126+
127+
const scipBinaryFile = path.join(projectRoot, options.output);
128+
const output = fs.openSync(scipBinaryFile, 'w');
129+
130+
if (options.index) {
131+
let indexer = new Indexer({
132+
...options,
133+
workspaceRoot: projectRoot,
134+
projectRoot,
135+
projectName,
136+
projectVersion,
137+
environment,
138+
writeIndex: (partialIndex: any): void => {
139+
fs.writeSync(output, partialIndex.serializeBinary());
140+
},
141+
});
142+
indexer.index();
143+
fs.close(output);
144+
}
145+
146+
const contents = fs.readFileSync(scipBinaryFile);
147+
const scipIndex = scip.Index.deserializeBinary(contents);
148+
149+
for (const doc of scipIndex.documents) {
150+
if (doc.relative_path.startsWith('..')) {
151+
continue;
152+
}
153+
154+
const inputPath = path.join(projectRoot, doc.relative_path);
155+
const input = Input.fromFile(inputPath);
156+
const obtained = formatSnapshot(input, doc, scipIndex.external_symbols);
157+
const relativeToInputDirectory = path.relative(projectRoot, inputPath);
158+
const outputPath = path.resolve(outputDirectory, snapshotDir, relativeToInputDirectory);
159+
160+
if (options.check) {
161+
diffSnapshot(outputPath, obtained);
162+
} else {
163+
writeSnapshot(outputPath, obtained);
164+
}
165+
}
166+
}
167+
}
168+
169+
export function main(argv: string[]): void {
170+
const command = mainCommand(indexAction, snapshotAction, (_) => {
171+
throw 'not yet implemented';
172+
// console.log('ENVIRONMENT OPTIONS', options);
173+
// console.log(getEnvironment(new Set(), '', undefined));
174+
});
175+
command.parse(argv);
176+
}

packages/pyright-scip/src/main.ts

Lines changed: 1 addition & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,3 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
3-
import * as child_process from 'child_process';
4-
5-
import { scip } from './scip';
6-
import { diffSnapshot, formatSnapshot, writeSnapshot } from './lib';
7-
import { Input } from './lsif-typescript/Input';
8-
import { join } from 'path';
9-
import { IndexOptions, SnapshotOptions, mainCommand } from './MainCommand';
10-
import { sendStatus, setQuiet, setShowProgressRateLimit } from './status';
11-
import { Indexer } from './indexer';
12-
import { exit } from 'process';
13-
14-
function indexAction(options: IndexOptions): void {
15-
setQuiet(options.quiet);
16-
if (options.showProgressRateLimit !== undefined) {
17-
setShowProgressRateLimit(options.showProgressRateLimit);
18-
}
19-
20-
const workspaceRoot = options.cwd;
21-
const snapshotDir = options.snapshotDir;
22-
const environment = options.environment;
23-
24-
const projectRoot = workspaceRoot;
25-
process.chdir(workspaceRoot);
26-
27-
// TODO: use setup.py / poetry to determine better projectName
28-
const projectName = options.projectName;
29-
if (!projectName || projectName == '') {
30-
console.warn('Must pass `--project-name`');
31-
return;
32-
}
33-
34-
// TODO: Use setup.py / poetry to determine better projectVersion
35-
// for now, the current hash works OK
36-
let projectVersion = options.projectVersion;
37-
if (!projectVersion || projectVersion === '') {
38-
// Default to current git hash
39-
try {
40-
projectVersion = child_process.execSync('git rev-parse HEAD').toString().trim();
41-
} catch (e) {
42-
console.warn('Must either pass `--project-version` or run from within a git repository');
43-
return;
44-
}
45-
}
46-
47-
const outputFile = path.join(projectRoot, options.output);
48-
const output = fs.openSync(outputFile, 'w');
49-
50-
sendStatus(`Indexing ${projectRoot} with version ${projectVersion}`);
51-
52-
try {
53-
let indexer = new Indexer({
54-
...options,
55-
workspaceRoot,
56-
projectRoot,
57-
projectName,
58-
projectVersion,
59-
environment,
60-
writeIndex: (partialIndex: scip.Index): void => {
61-
fs.writeSync(output, partialIndex.serializeBinary());
62-
},
63-
});
64-
65-
indexer.index();
66-
} catch (e) {
67-
console.warn(
68-
'\n\nExperienced Fatal Error While Indexing:\nPlease create an issue at github.com/sourcegraph/scip-python:',
69-
e
70-
);
71-
exit(1);
72-
}
73-
74-
fs.close(output);
75-
76-
if (snapshotDir) {
77-
sendStatus(`Writing snapshot from index: ${outputFile}`);
78-
79-
const scipIndex = scip.Index.deserializeBinary(fs.readFileSync(outputFile));
80-
for (const doc of scipIndex.documents) {
81-
if (doc.relative_path.startsWith('..')) {
82-
console.log('Skipping Doc:', doc.relative_path);
83-
continue;
84-
}
85-
86-
const inputPath = path.join(projectRoot, doc.relative_path);
87-
const input = Input.fromFile(inputPath);
88-
const obtained = formatSnapshot(input, doc, scipIndex.external_symbols);
89-
const relativeToInputDirectory = path.relative(projectRoot, inputPath);
90-
const outputPath = path.resolve(snapshotDir, relativeToInputDirectory);
91-
writeSnapshot(outputPath, obtained);
92-
}
93-
}
94-
}
95-
96-
function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
97-
setQuiet(options.quiet);
98-
if (options.showProgressRateLimit !== undefined) {
99-
setShowProgressRateLimit(options.showProgressRateLimit);
100-
}
101-
102-
console.log('... Snapshotting ... ');
103-
const projectName = options.projectName;
104-
const projectVersion = options.projectVersion;
105-
const environment = options.environment ? path.resolve(options.environment) : undefined;
106-
107-
const snapshotOnly = options.only;
108-
109-
const inputDirectory = path.resolve(join(snapshotRoot, 'input'));
110-
const outputDirectory = path.resolve(join(snapshotRoot, 'output'));
111-
112-
// Either read all the directories or just the one passed in by name
113-
let snapshotDirectories = fs.readdirSync(inputDirectory);
114-
if (snapshotOnly) {
115-
snapshotDirectories = [snapshotOnly];
116-
}
117-
118-
for (const snapshotDir of snapshotDirectories) {
119-
let projectRoot = join(inputDirectory, snapshotDir);
120-
if (!fs.lstatSync(projectRoot).isDirectory()) {
121-
continue;
122-
}
123-
124-
projectRoot = path.resolve(projectRoot);
125-
process.chdir(projectRoot);
126-
127-
const scipBinaryFile = path.join(projectRoot, options.output);
128-
const output = fs.openSync(scipBinaryFile, 'w');
129-
130-
if (options.index) {
131-
let indexer = new Indexer({
132-
...options,
133-
workspaceRoot: projectRoot,
134-
projectRoot,
135-
projectName,
136-
projectVersion,
137-
environment,
138-
writeIndex: (partialIndex: any): void => {
139-
fs.writeSync(output, partialIndex.serializeBinary());
140-
},
141-
});
142-
indexer.index();
143-
fs.close(output);
144-
}
145-
146-
const contents = fs.readFileSync(scipBinaryFile);
147-
const scipIndex = scip.Index.deserializeBinary(contents);
148-
149-
for (const doc of scipIndex.documents) {
150-
if (doc.relative_path.startsWith('..')) {
151-
continue;
152-
}
153-
154-
const inputPath = path.join(projectRoot, doc.relative_path);
155-
const input = Input.fromFile(inputPath);
156-
const obtained = formatSnapshot(input, doc, scipIndex.external_symbols);
157-
const relativeToInputDirectory = path.relative(projectRoot, inputPath);
158-
const outputPath = path.resolve(outputDirectory, snapshotDir, relativeToInputDirectory);
159-
160-
if (options.check) {
161-
diffSnapshot(outputPath, obtained);
162-
} else {
163-
writeSnapshot(outputPath, obtained);
164-
}
165-
}
166-
}
167-
}
168-
169-
export function main(argv: string[]): void {
170-
const command = mainCommand(indexAction, snapshotAction, (_) => {
171-
throw 'not yet implemented';
172-
// console.log('ENVIRONMENT OPTIONS', options);
173-
// console.log(getEnvironment(new Set(), '', undefined));
174-
});
175-
command.parse(argv);
176-
}
1+
import { main } from './main-impl';
1772

1783
main(process.argv);

0 commit comments

Comments
 (0)