Skip to content
Merged
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ like the ranking command. The thresholds:
| `functionDepDegree` | a function's DepDegree is above it | off | off |
| `fileNcss` | a file's NCSS is above it | 400 | 1000 |
| `duplicateLines` | a duplicated block holds at least this many lines matched in a copy | 15 | off |
| `testNcssPercent` | the target's test NCSS, as a percentage of its other NCSS, is above | off | 30 |
| `testNcssPercent` | the target's test NCSS, as a percentage of its other NCSS, is above | 30 | off |

`testNcssPercent` holds the test code of the checked directory in proportion to its production
code: the NCSS of its test files against that of the other files the check measures. A file counts
Expand All @@ -126,7 +126,11 @@ warning says so). With `--base`, it is reported only when the share exceeds its
change touches a test: git lists a path as changed since the merge-base or untracked, and the
check's files still include it under the target as a test by its name. A change that
touches no test, or only deletes or moves tests out, is never blamed for a share it did not raise;
one that does is told to delete or merge tests so that it adds no test code. No directive allows it, and no override can change
one that does is told to keep a test it adds only when the project's test rule (the project's own
criteria for when a test is worth having) justifies it, and to delete or merge the others. Without
`--base`, a violation states how much test NCSS is above the strictest active limit and asks to review the tests
against the project's test rule and delete or merge those it does not justify.
No directive allows it, and no override can change
its limit: `"testNcssPercent": "off"` in the config's `rules`, or `--warn testNcssPercent=off
--error testNcssPercent=off` on the command line, disables it at both levels.

Expand All @@ -143,8 +147,9 @@ thresholds that are off, nesting depth flags no function that cognitive complexi
cyclomatic complexity and Halstead difficulty flag functions rated like unflagged ones; Halstead
volume, Halstead effort, and DepDegree are high mostly where a function is long or takes many
parameters, which the limits above already flag.
The `testNcssPercent` default sits just below the median test share, in lines, of 50 of the
organization's repositories; no ratings back it.
The `testNcssPercent` default is a warning just below the median test share in NCSS (31.6%) of 62
checkouts of the organization's repositories on one developer's machine, and no error: no ratings back it, so it asks for a review of the tests
rather than marking code to fix.
[How the default thresholds were calibrated](docs/threshold-calibration.md) holds the corpus, the
distributions per language, the ratings, and the reasoning per threshold.

Expand Down Expand Up @@ -366,9 +371,8 @@ touches.
kind `file`, the first and last line of the file; for kind `project`, only `file`, the checked
directory relative to the repository root (`.` for the root);
- `testNcss`, `productionNcss`, and `excessTestNcss`: only for kind `project`; the NCSS of the
test files and of the other files, and the test NCSS above the strictest active limit (what a
whole project deletes or merges to clear every level, which can exceed what clears the level
reported);
test files and of the other files, and the test NCSS above the strictest active limit (which
can exceed what is above the limit of the level reported);
- `name`: only for kind `function`; a member's name follows the type, module, or object it
belongs to (`Rules.decide`), and a function without a name is `<anonymous>`;
- `exceeded`: every violated threshold with its `metric` (the rule's name in the config and on
Expand Down Expand Up @@ -418,7 +422,7 @@ with `--config`). The following config reproduces every built-in default:
"functionDepDegree": "off",
"fileNcss": { "warn": 400, "error": 1000 },
"duplicateLines": { "warn": 15, "error": "off" },
"testNcssPercent": { "warn": "off", "error": 30 }
"testNcssPercent": { "warn": 30, "error": "off" }
},
"overrides": [],
"exclude": [],
Expand Down
27 changes: 20 additions & 7 deletions docs/threshold-calibration.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,26 @@ With these limits, `check` reports 464 errors and 2,430 warnings in the calibrat

## Test share

`testNcssPercent` defaults to an error above 30, and to no warning. It was set in November 2026
from the 50 WillBooster repositories on a developer's machine whose production code exceeds 3,000
lines: their test code, counted in lines rather than NCSS, was 33% of their production code at the
median. The limit sits just below that median. Its effect in NCSS was not measured on the corpus,
which would tell how many projects it holds and whether halving the tests of the median project
brings it under the limit: a line of test code can hold more or fewer statements than a line of
production code.
`testNcssPercent` defaults to a warning above 30, and to no error. The value was first set in
November 2026 from the 50 WillBooster repositories on a developer's machine whose production code
exceeds 3,000 lines: their test code, counted in lines rather than NCSS, was 33% of their production
code at the median.

The share was then measured in NCSS on the 62 checkouts of WillBooster repositories on one
developer's machine whose production NCSS is at least 1,000 (second clones and forks among them), with
`check` of code-gauge 11.1.0 reporting every share as a warning:

| Percentile | Test NCSS percent |
| ---------- | ----------------: |
| 50th | 31.6 |
| 75th | 60.8 |
| 90th | 76.2 |

32 of the 62 (52%) are above 30%, and 17 (27%) above 60%. A limit of 30 thus sits just below the
median in NCSS too. No ratings tell whether a share above it holds tests worth deleting, so it is a warning,
which marks code worth simplifying when it is touched, and not an error, which would mark half of
these checkouts as code to fix: exceeding it asks for a review of the tests against the
project's test rule.

## Duplicated blocks measured in matched lines

Expand Down
6 changes: 3 additions & 3 deletions src/checkCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,9 @@ function describeViolation(violation: Violation, exceeded: string, checksChange:
switch (violation.kind) {
case 'project': {
const remedy = checksChange
? `this change edits tests while test code exceeds ${violation.exceeded[0]?.limit}% of production code: delete or merge tests so the change adds no test code`
: `delete or merge ${violation.excessTestNcss} test NCSS`;
return `${violation.file}: ${exceeded}, test NCSS ${violation.testNcss} of production NCSS ${violation.productionNcss}; ${remedy}`;
? `; this change edits tests while test code exceeds ${violation.exceeded[0]?.limit}% of production code: keep a test it adds only when the project's test rule justifies it, and delete or merge the others`
: `, ${violation.excessTestNcss} above the strictest limit; review the tests against the project's test rule and delete or merge those it does not justify`;
return `${violation.file}: ${exceeded}, test NCSS ${violation.testNcss} of production NCSS ${violation.productionNcss}${remedy}`;
}
case 'file': {
return `${violation.file}: ${exceeded}`;
Expand Down
9 changes: 5 additions & 4 deletions src/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ export interface TestShare {
}

/**
* Test code is kept in proportion to the production code it checks. The limit applies to the whole
* checked project, so no override for file patterns can change it.
* Test code in proportion to the production code it checks; exceeding the limit calls for reviewing
* tests against the project's test rule. The limit applies to the whole checked project, so no
* override for file patterns can change it.
*/
export const testShareThreshold: Threshold<TestShare> = {
metric: 'testNcssPercent',
label: 'test NCSS percent',
defaultLimits: { warning: Infinity, error: 30 },
hint: 'delete or merge tests as the violation says, in the same change.',
defaultLimits: { warning: 30, error: Infinity },
hint: "review the tests the violation names (with --base, those the change adds) against the project's test rule and delete or merge those it does not justify.",
measure: ({ testNcss, productionNcss }) => (testNcss * 100) / productionNcss,
};

Expand Down
17 changes: 4 additions & 13 deletions test/e2e/checkCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1257,13 +1257,8 @@ describe('code-gauge check: test share', () => {

it('reports a project whose test code exceeds 30% of its production code, unless the config disables it', () => {
writeSource('test/calc.test.ts', testCode(60));
const result = runCheck([]);
expect(result.status).toBe(1);
expect(result.stdout).toMatch(
/^error: \.: test NCSS percent [\d.]+ \(max 30\), test NCSS \d+ of production NCSS \d+; delete or merge \d+ test NCSS$/mu
);
const [violation] = projectViolations([]);
expect(violation).toMatchObject({ kind: 'project', file: '.', level: 'error' });
expect(violation).toMatchObject({ kind: 'project', file: '.', level: 'warning' });
expect((violation?.testNcss ?? 0) * 100).toBeGreaterThan((violation?.productionNcss ?? 0) * 30);

writeConfig({ rules: { ...baseConfig.rules, testNcssPercent: 'off' } });
Expand All @@ -1272,7 +1267,7 @@ describe('code-gauge check: test share', () => {

it.each([
['the default limit', [], 0],
['a warning limit stricter than the error limit', ['--warn', 'testNcssPercent=10'], 0],
['an error limit looser than the warning limit', ['--error', 'testNcssPercent=50'], 0],
// 375 production NCSS at 18.4% admit 69 test NCSS, which multiplying and flooring puts at 68.
['a fractional limit', ['--error', 'testNcssPercent=18.4'], 326],
])('names the least test NCSS whose deletion clears the share under %s', (_, limits, extraProduction) => {
Expand Down Expand Up @@ -1380,11 +1375,7 @@ describe('code-gauge check: test share', () => {
expect(projectViolations(['--base', 'HEAD']), 'a change that only deletes tests').toEqual([]);

writeSource('test/calc.test.ts', testCode(59));
const changed = runCheck(['--base', 'HEAD']);
expect(changed.stdout).toMatch(
/^error: \.: test NCSS percent [\d.]+ \(max 30\), test NCSS \d+ of production NCSS \d+; this change edits tests while test code exceeds 30% of production code: delete or merge tests so the change adds no test code$/mu
);
expect(projectViolations(['--base', 'HEAD'])).toMatchObject([{ kind: 'project', level: 'error' }]);
expect(projectViolations(['--base', 'HEAD'])).toMatchObject([{ kind: 'project', level: 'warning' }]);
});

it('reports the share of a targeted directory with --base only for a test edited inside it', () => {
Expand Down Expand Up @@ -1474,7 +1465,7 @@ describe('code-gauge check: test share', () => {
chmodSync(path.join(repoDir, 'private'), 0o000);
try {
const result = runCheck(['--json']);
expect(result.status, result.stderr).toBe(1);
expect(result.status, result.stderr).toBe(0);
expect((JSON.parse(result.stdout) as ProjectReport).violations).toContainEqual(
expect.objectContaining({ kind: 'project' })
);
Expand Down
Loading