Skip to content

Commit 0e03704

Browse files
committed
fix(cli): support single file source argument
closes #1384
1 parent d303e4e commit 0e03704

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎src/index-cli.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ Note: Certain tabs will only be shown if applicable to a given dependency`,
964964
}
965965

966966
let sourceFolder;
967+
let sourceFileInput: string | undefined;
967968
if (program.args.length > 0) {
968969
/**
969970
* tsconfig file provided with source folder in arg
@@ -978,14 +979,22 @@ Note: Certain tabs will only be shown if applicable to a given dependency`,
978979
);
979980
}
980981

981-
sourceFolder = program.args[0];
982-
if (!FileEngine.existsSync(sourceFolder)) {
982+
const providedSourcePath = program.args[0];
983+
if (!FileEngine.existsSync(providedSourcePath)) {
983984
logger.error(
984-
`Provided source folder ${sourceFolder} was not found in the current directory`,
985+
`Provided source ${providedSourcePath} was not found in the current directory`,
985986
);
986987
process.exit(1);
987988
} else {
988-
logger.info("Using provided source folder");
989+
const sourceStats = fs.statSync(providedSourcePath);
990+
if (sourceStats.isFile()) {
991+
sourceFolder = path.dirname(providedSourcePath);
992+
sourceFileInput = providedSourcePath;
993+
logger.info("Using provided source file");
994+
} else {
995+
sourceFolder = providedSourcePath;
996+
logger.info("Using provided source folder");
997+
}
989998
}
990999
}
9911000

@@ -1029,6 +1038,12 @@ Note: Certain tabs will only be shown if applicable to a given dependency`,
10291038
includeFiles = [...includeFiles, ...scannedFiles];
10301039
}
10311040

1041+
if (sourceFileInput) {
1042+
// Restrict scanning to the provided single file to avoid treating it as a directory.
1043+
includeFiles = [path.basename(sourceFileInput)];
1044+
scannedFiles = [];
1045+
}
1046+
10321047
if (!includeFiles.length) {
10331048
includeFiles = INCLUDE_PATTERNS;
10341049
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect } from "chai";
2+
import { temporaryDir, shell } from "../helpers";
3+
4+
const tmp = temporaryDir();
5+
6+
describe("CLI issue #1384 - single file coverage command", () => {
7+
const distFolder = tmp.name + "-issue-1384";
8+
9+
before(function (done) {
10+
tmp.create(distFolder);
11+
done();
12+
});
13+
14+
after(() => tmp.clean(distFolder));
15+
16+
it("should accept a single source file positional argument without ENOTDIR", () => {
17+
const command = shell("node", [
18+
"./bin/index-cli.js",
19+
"./test/fixtures/sample-files/bar.component.ts",
20+
"-p",
21+
"./test/fixtures/sample-files/tsconfig.simple.json",
22+
"--silent",
23+
"--coverageTest",
24+
"0",
25+
"--coverageTestShowOnlyFailed",
26+
"false",
27+
"-d",
28+
distFolder,
29+
]);
30+
31+
expect(command.status).to.equal(0);
32+
expect(command.stderr.toString()).to.equal("");
33+
expect(command.stdout.toString()).to.not.contain("ENOTDIR");
34+
});
35+
});

0 commit comments

Comments
 (0)