-
Notifications
You must be signed in to change notification settings - Fork 23
Add tests for command-line parser #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fe1e5b0
Add tests for command-line parser
olafurpg 91d24be
Step 1: rename infertsconfig file to fix capitalization
olafurpg 5128cc1
Step 2: rename infertsconfig file back with correct capitalization
olafurpg 4d55614
Fix parsing of --cwd and address review comments
olafurpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { test } from 'uvu' | ||
import * as assert from 'uvu/assert' | ||
|
||
import { mainCommand, MultiProjectOptions } from './CommandLineOptions' | ||
|
||
function checkIndexParser( | ||
args: string[], | ||
expectedOptions: Partial<MultiProjectOptions>, | ||
expectedProjects?: string[] | ||
): void { | ||
test(args.join(' '), () => { | ||
let isAssertionTriggered = false | ||
const actualArguments = ['node', 'lsif-typescript.js', 'index', ...args] | ||
mainCommand((projects, options) => { | ||
assert.equal(options, { ...options, ...expectedOptions }) | ||
if (expectedProjects) { | ||
assert.equal(projects, expectedProjects) | ||
} | ||
isAssertionTriggered = true | ||
}).parse(actualArguments) | ||
assert.ok(isAssertionTriggered) | ||
}) | ||
} | ||
|
||
// defaults | ||
checkIndexParser([], { | ||
progressBar: true, | ||
cwd: process.cwd(), | ||
inferTsconfig: false, | ||
output: 'dump.lsif-typed', | ||
yarnWorkspaces: false, | ||
}) | ||
|
||
checkIndexParser(['--cwd', 'qux'], { cwd: 'qux' }) | ||
checkIndexParser(['--yarn-workspaces'], { yarnWorkspaces: true }) | ||
checkIndexParser(['--infer-tsconfig'], { inferTsconfig: true }) | ||
checkIndexParser(['--no-progress-bar'], { progressBar: false }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Command } from 'commander' | ||
|
||
import packageJson from '../package.json' | ||
|
||
import * as lsif from './lsif' | ||
|
||
/** Configuration options to index a multi-project workspace. */ | ||
export interface MultiProjectOptions { | ||
inferTsconfig: boolean | ||
progressBar: boolean | ||
yarnWorkspaces: boolean | ||
cwd: string | ||
output: string | ||
indexedProjects: Set<string> | ||
} | ||
|
||
/** Configuration options to index a single TypeScript project. */ | ||
export interface ProjectOptions extends MultiProjectOptions { | ||
projectRoot: string | ||
projectDisplayName: string | ||
writeIndex: (index: lsif.lib.codeintel.lsiftyped.Index) => void | ||
} | ||
|
||
export function mainCommand( | ||
indexAction: (projects: string[], otpions: MultiProjectOptions) => void | ||
): Command { | ||
const command = new Command() | ||
command | ||
.name('lsif-typescript') | ||
.version(packageJson.version) | ||
.description( | ||
'LSIF indexer for TypeScript and JavaScript\nFor usage examples, see https://github.com/sourcegraph/lsif-typescript/blob/main/README.md' | ||
) | ||
command | ||
.command('index') | ||
.option('--cwd <path>', 'the working directory', process.cwd()) | ||
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false) | ||
.option( | ||
'--infer-tsconfig', | ||
"whether to infer the tsconfig.json file, if it's missing", | ||
false | ||
) | ||
.option('--output <path>', 'path to the output file', 'dump.lsif-typed') | ||
.option('--no-progress-bar', 'whether to disable the progress bar') | ||
.argument('[projects...]') | ||
.action((parsedProjects, parsedOptions) => { | ||
indexAction( | ||
parsedProjects as string[], | ||
parsedOptions as MultiProjectOptions | ||
) | ||
}) | ||
return command | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import * as fs from 'fs' | |
import * as path from 'path' | ||
|
||
/** | ||
* To limit the risk of making the `inferTsConfig` run for a very long time, we | ||
* To limit the risk of making the `inferTsconfig` run for a very long time, we | ||
* stop the file traversal after visiting this number of files. | ||
*/ | ||
const maximumFileTraversalCount = 1_000 | ||
|
@@ -19,7 +19,7 @@ export const noJsConfig = '{}' | |
* If the directory contains at least one `*.{ts,tsx}` file then the config will be empty (`{}`). | ||
* If the directory doesn't contains one `*.{ts,tsx}` file then the config will | ||
*/ | ||
export function inferTsConfig(projectPath: string): string { | ||
export function inferTsconfig(projectPath: string): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I apologize for mixing this unrelated diff into the PR. It's quite painful to correct the capitalization of files on macOS and I didn't feel like doing it again to have a cleaner PR |
||
let hasTypeScriptFile = false | ||
let hasJavaScriptFile = false | ||
let visitedFileCount = 0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, I didn't know this was a thing you could do so easily in TypeScript. (The
Partial<>
type)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very handy for cases like this! TypeScript includes several utility types https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype but you can go a bit overboard with them so use with care.