Avoid analyzing excluded language files for line counting

This change passes in a list of file types to the line counting
analysis. These are the languages for the databases being analyzed.
Line count analysis is restricted to these files.
This commit is contained in:
Andrew Eisenberg 2021-04-28 14:57:44 -07:00
parent 5c0a38d7e4
commit ee2346270d
31 changed files with 436 additions and 49 deletions

View file

@ -20,7 +20,7 @@ setupTests(test);
// paths are set in the database analyze invocation.
test("status report fields and search path setting", async (t) => {
const mockLinesOfCode = Object.entries(Language).reduce((obj, [lang], i) => {
// use a different line count for each languaged
// use a different line count for each language
obj[lang] = i + 1;
return obj;
}, {});

View file

@ -151,7 +151,7 @@ export async function runQueries(
// config.paths specifies external directories. the current
// directory is included in the analysis by default. Replicate
// that here.
["**"].concat(config.paths || []),
config.paths,
config.pathsIgnore,
config.languages,
logger

81
src/count-loc.test.ts Normal file
View file

@ -0,0 +1,81 @@
import * as path from "path";
import test from "ava";
import { countLoc } from "./count-loc";
import { getRunnerLogger } from "./logging";
import { setupTests } from "./testing-utils";
setupTests(test);
test("ensure lines of code works for cpp and js", async (t) => {
const results = await countLoc(
path.join(__dirname, "../tests/multi-language-repo"),
[],
[],
["cpp", "js"],
getRunnerLogger(true)
);
t.deepEqual(results, {
cpp: 6,
js: 3,
});
});
test("ensure lines of code can handle undefined language", async (t) => {
const results = await countLoc(
path.join(__dirname, "../tests/multi-language-repo"),
[],
[],
["rb", "py", "hucairz"],
getRunnerLogger(true)
);
t.deepEqual(results, {
rb: 6,
py: 5,
});
});
test("ensure lines of code can handle empty languages", async (t) => {
const results = await countLoc(
path.join(__dirname, "../tests/multi-language-repo"),
[],
[],
[],
getRunnerLogger(true)
);
t.deepEqual(results, {});
});
test("ensure lines of code can handle includes", async (t) => {
// note that "**" is always included. The includes are for extra
// directories outside the normal structure.
const results = await countLoc(
path.join(__dirname, "../tests/multi-language-repo"),
["../../src/testdata"],
[],
["js"],
getRunnerLogger(true)
);
t.deepEqual(results, {
js: 15,
});
});
test("ensure lines of code can handle exclude", async (t) => {
const results = await countLoc(
path.join(__dirname, "../tests/multi-language-repo"),
[],
["**/*.py"],
["rb", "py"],
getRunnerLogger(true)
);
t.deepEqual(results, {
rb: 6,
});
});

View file

@ -15,6 +15,17 @@ const supportedLanguages = {
typescript: "js",
};
const supportedLanguagesReversed = Object.entries(supportedLanguages).reduce(
(obj, [key, value]) => {
if (!obj[value]) {
obj[value] = [];
}
obj[value].push(key);
return obj;
},
{}
);
/**
* Count the lines of code of the specified language using the include
* and exclude glob paths.
@ -34,8 +45,11 @@ export async function countLoc(
): Promise<Record<string, number>> {
const result = await new LocDir({
cwd,
include,
include: ["**"].concat(include || []),
exclude,
analysisLanguages: dbLanguages.flatMap(
(lang) => supportedLanguagesReversed[lang]
),
}).loadInfo();
// The analysis counts LoC in all languages. We need to

4
src/testdata/testFile3.ts vendored Normal file
View file

@ -0,0 +1,4 @@
var a;
var b;
var c;
var d;