@crown3/repo-doctor 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +40 -1
- package/UPSTREAM.md +20 -0
- package/bin/repo-doctor.mjs +9 -0
- package/dist/check-comments.d.ts +3 -0
- package/dist/check-docs.d.ts +16 -0
- package/dist/check-runs.d.ts +23 -0
- package/dist/cli.d.ts +16 -0
- package/dist/cli.js +1743 -0
- package/dist/comments.d.ts +17 -0
- package/dist/config.d.ts +152 -0
- package/dist/file-kinds.d.ts +8 -0
- package/dist/files.d.ts +8 -0
- package/dist/format-sh.d.ts +6 -0
- package/dist/g.js +360 -0
- package/dist/git.d.ts +17 -0
- package/dist/go-comments.d.ts +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +16 -0
- package/dist/python.d.ts +18 -0
- package/dist/quality.d.ts +39 -0
- package/dist/review-comments.d.ts +14 -0
- package/dist/rules.d.ts +8 -0
- package/dist/run-if-main.d.ts +2 -0
- package/dist/scrub-git-hook-env.d.ts +1 -0
- package/dist/scrub-git-hook-env.js +2 -0
- package/dist/settings.d.ts +19 -0
- package/dist/shard.d.ts +10 -0
- package/dist/sites.d.ts +9 -0
- package/dist/units.d.ts +30 -0
- package/package.json +61 -3
- package/python/crapper/LICENSE +7 -0
- package/python/crapper/README.md +93 -0
- package/python/crapper/pyproject.toml +28 -0
- package/python/crapper/src/crapper/__init__.py +3 -0
- package/python/crapper/src/crapper/__main__.py +4 -0
- package/python/crapper/src/crapper/analyze.py +56 -0
- package/python/crapper/src/crapper/cli.py +334 -0
- package/python/crapper/src/crapper/coverage.py +596 -0
- package/python/crapper/src/crapper/crap.py +47 -0
- package/python/crapper/src/crapper/discover.py +116 -0
- package/python/crapper/src/crapper/languages/__init__.py +27 -0
- package/python/crapper/src/crapper/languages/clojure.py +451 -0
- package/python/crapper/src/crapper/languages/golang.py +143 -0
- package/python/crapper/src/crapper/languages/java.py +117 -0
- package/python/crapper/src/crapper/languages/python.py +133 -0
- package/python/crapper/src/crapper/languages/rust.py +190 -0
- package/python/crapper/src/crapper/languages/treesitter.py +82 -0
- package/python/crapper/src/crapper/languages/typescript.py +399 -0
- package/python/crapper/src/crapper/metrics.py +59 -0
- package/python/crapper/src/crapper/model.py +32 -0
- package/python/crapper/src/crapper/report.py +42 -0
- package/python/crapper/src/crapper/runners.py +596 -0
- package/python/dryer/LICENSE +7 -0
- package/python/dryer/README.md +115 -0
- package/python/dryer/pyproject.toml +29 -0
- package/python/dryer/src/dryer/__init__.py +3 -0
- package/python/dryer/src/dryer/__main__.py +4 -0
- package/python/dryer/src/dryer/astnorm.py +391 -0
- package/python/dryer/src/dryer/cli.py +324 -0
- package/python/dryer/src/dryer/clojure_read.py +519 -0
- package/python/dryer/src/dryer/discover.py +121 -0
- package/python/dryer/src/dryer/extract.py +204 -0
- package/python/dryer/src/dryer/model.py +34 -0
- package/python/dryer/src/dryer/report.py +74 -0
- package/python/dryer/src/dryer/scan.py +91 -0
- package/python/dryer/src/dryer/shape.py +108 -0
- package/python/dryer/src/dryer/treesitter.py +47 -0
- package/python/mutator/LICENSE +7 -0
- package/python/mutator/README.md +123 -0
- package/python/mutator/pyproject.toml +28 -0
- package/python/mutator/src/mutator/__init__.py +3 -0
- package/python/mutator/src/mutator/__main__.py +4 -0
- package/python/mutator/src/mutator/cli.py +591 -0
- package/python/mutator/src/mutator/coverage.py +233 -0
- package/python/mutator/src/mutator/crapper_link.py +36 -0
- package/python/mutator/src/mutator/edn.py +239 -0
- package/python/mutator/src/mutator/engine.py +356 -0
- package/python/mutator/src/mutator/functions.py +214 -0
- package/python/mutator/src/mutator/metrics.py +344 -0
- package/python/mutator/src/mutator/model.py +83 -0
- package/python/mutator/src/mutator/report.py +74 -0
- package/python/mutator/src/mutator/runner.py +248 -0
- package/python/mutator/src/mutator/sites.py +376 -0
- package/python/mutator/src/mutator/workers.py +446 -0
- package/python/requirements.lock +12 -0
package/dist/rules.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Settings } from './settings.js';
|
|
2
|
+
export interface Invocation {
|
|
3
|
+
cwd: string;
|
|
4
|
+
args: string[];
|
|
5
|
+
load: (root: string) => Promise<Settings>;
|
|
6
|
+
}
|
|
7
|
+
export type Rule = (run: Invocation) => Promise<number>;
|
|
8
|
+
export declare const RULES: Record<string, Rule>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Checked, type DryConfig } from './config.js';
|
|
2
|
+
import { type Unit } from './units.js';
|
|
3
|
+
export declare const CONFIG_FILE = "repo-doctor.config.mjs";
|
|
4
|
+
export declare class UsageError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export interface Quality {
|
|
7
|
+
units: Unit[];
|
|
8
|
+
dry: Required<DryConfig>;
|
|
9
|
+
}
|
|
10
|
+
export interface Settings extends Omit<Checked, 'workspaces' | 'quality'> {
|
|
11
|
+
workspaces: string[];
|
|
12
|
+
quality: false | Quality;
|
|
13
|
+
}
|
|
14
|
+
/** Checks a loaded config and resolves it against the repo at root: workspaces discovered, units with their final settings. */
|
|
15
|
+
export declare function resolveSettings(root: string, config?: unknown): Settings;
|
|
16
|
+
/** Loads the config module (file from cwd, or repo-doctor.config.mjs in root) and resolves it. */
|
|
17
|
+
export declare function loadSettings(root: string, file: string | undefined, cwd?: string): Promise<Settings>;
|
|
18
|
+
/** The settings as JSON, RegExps and functions written as strings. */
|
|
19
|
+
export declare const settingsJson: (settings: Settings) => string;
|
package/dist/shard.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Shard {
|
|
2
|
+
index: number;
|
|
3
|
+
count: number;
|
|
4
|
+
}
|
|
5
|
+
export declare function parseShard(text: string): Shard;
|
|
6
|
+
export interface Sized {
|
|
7
|
+
file: string;
|
|
8
|
+
cost: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function deal(entries: Sized[], { index, count }: Shard): string[];
|
package/dist/sites.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type Touched = (file: string, line: number) => boolean;
|
|
2
|
+
export interface Site {
|
|
3
|
+
status: string;
|
|
4
|
+
file: string;
|
|
5
|
+
line: number;
|
|
6
|
+
text: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const sitesIn: (output: string) => Site[];
|
|
9
|
+
export declare function splitByChange(dir: string, sites: Site[], touched: Touched): string[];
|
package/dist/units.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { QualityConfig } from './config.js';
|
|
2
|
+
export type Choice = {
|
|
3
|
+
kind: 'command';
|
|
4
|
+
command: string;
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'npm script';
|
|
7
|
+
command: string | undefined;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'tool default';
|
|
10
|
+
};
|
|
11
|
+
export interface Unit {
|
|
12
|
+
dir: string;
|
|
13
|
+
paths: string[];
|
|
14
|
+
exclude: string[];
|
|
15
|
+
coverage: Choice;
|
|
16
|
+
test: Choice;
|
|
17
|
+
crap: {
|
|
18
|
+
threshold: number;
|
|
19
|
+
};
|
|
20
|
+
mutate: {
|
|
21
|
+
minScore?: number;
|
|
22
|
+
};
|
|
23
|
+
dropped?: true;
|
|
24
|
+
}
|
|
25
|
+
/** The workspace directories the root's pnpm, npm, go.work and Cargo workspace files name that hold a manifest. */
|
|
26
|
+
export declare const discovered: (root: string) => string[];
|
|
27
|
+
/** The workspace directories: the config's globs, or else the discovered ones. */
|
|
28
|
+
export declare const workspaces: (root: string, globs: string[] | undefined) => string[];
|
|
29
|
+
/** The units: the root, every workspace and every directory quality.units names, a false one dropped. */
|
|
30
|
+
export declare function resolve(root: string, quality: QualityConfig, dirs: string[], exclude?: string[]): Unit[];
|
package/package.json
CHANGED
|
@@ -1,14 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crown3/repo-doctor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Repo checks and ub-mode quality gates shared across repos, driven by one config file: the comment policy, shell formatting, agent docs, where hooks and CI run each script, CRAP scores, duplicate code and mutation testing.",
|
|
5
5
|
"license": "0BSD",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "git+https://github.com/crown3/cc-origins.git",
|
|
9
10
|
"directory": "packages/repo-doctor"
|
|
10
11
|
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"lint",
|
|
14
|
+
"comments",
|
|
15
|
+
"docs",
|
|
16
|
+
"ci",
|
|
17
|
+
"crap",
|
|
18
|
+
"mutation-testing",
|
|
19
|
+
"duplicate-code",
|
|
20
|
+
"quality",
|
|
21
|
+
"ub-mode"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=24"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./scrub-git-hook-env": "./dist/scrub-git-hook-env.js"
|
|
32
|
+
},
|
|
33
|
+
"bin": {
|
|
34
|
+
"repo-doctor": "bin/repo-doctor.mjs"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"bin",
|
|
38
|
+
"dist",
|
|
39
|
+
"python/requirements.lock",
|
|
40
|
+
"python/*/LICENSE",
|
|
41
|
+
"python/*/README.md",
|
|
42
|
+
"python/*/pyproject.toml",
|
|
43
|
+
"python/*/src/**/*.py",
|
|
44
|
+
"README.md",
|
|
45
|
+
"UPSTREAM.md"
|
|
46
|
+
],
|
|
47
|
+
"sideEffects": [
|
|
48
|
+
"./dist/scrub-git-hook-env.js"
|
|
49
|
+
],
|
|
11
50
|
"publishConfig": {
|
|
12
51
|
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@wasm-fmt/shfmt": "0.2.7",
|
|
55
|
+
"oxc-parser": "0.156.0",
|
|
56
|
+
"yaml": "2.9.1"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@rslib/core": "1.0.7",
|
|
60
|
+
"@rstest/core": "0.12.6",
|
|
61
|
+
"@types/node": "24.19.2",
|
|
62
|
+
"typescript": "7.0.2"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "rslib build",
|
|
66
|
+
"typecheck": "tsc -p . && tsc -p tsconfig.build.json --noEmit",
|
|
67
|
+
"test": "rstest",
|
|
68
|
+
"test:e2e": "rslib build && rstest -c rstest.e2e.config.mjs",
|
|
69
|
+
"test:tools": "node tests/pytest.ts",
|
|
70
|
+
"coverage": "rstest --coverage"
|
|
13
71
|
}
|
|
14
|
-
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
BSD Zero Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 by Robert C. Martin <unclebob@cleancoder.com>
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# crapper
|
|
2
|
+
|
|
3
|
+
CRAP scores for Clojure, Java, Go, TypeScript, Rust, and Python. One run detects the language of each source file, applies that language's complexity and coverage rules, and writes the snapshot [uml-viewer](https://github.com/unclebob/uml-viewer) already reads.
|
|
4
|
+
|
|
5
|
+
The formula is the one from [crap4clj](https://github.com/unclebob/crap4clj), [crap4java](https://github.com/unclebob/crap4java), and [crap4go](https://github.com/unclebob/crap4go):
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
CRAP = CC² × (1 − coverage)³ + CC
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`CC` is cyclomatic complexity. `coverage` is the fraction of the function exercised by tests. A score of 1–5 is low risk, 5–30 is worth a look, and 30+ is complex and under-tested.
|
|
12
|
+
|
|
13
|
+
## Run
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
./crapper
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The first run creates `.venv` and installs the tool. From a project root it walks the tree, skips `test`, `spec`, `vendor`, `node_modules`, and `target`, and writes two results:
|
|
20
|
+
|
|
21
|
+
- a table on stdout, worst score first
|
|
22
|
+
- `.metrics/crap.edn`, replaced on every successful analysis
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
./crapper --no-coverage # complexity only; coverage is N/A
|
|
26
|
+
./crapper --use-existing-coverage # read reports already on disk
|
|
27
|
+
./crapper src/demo/core.clj src/ui # these files and trees
|
|
28
|
+
./crapper --changed # git additions and edits
|
|
29
|
+
./crapper combat # path fragment, same idea as crap4clj
|
|
30
|
+
./crapper --threshold 30 # exit 2 when the worst score is higher
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Snapshot
|
|
34
|
+
|
|
35
|
+
`.metrics/crap.edn` has the same shape crap4clj writes:
|
|
36
|
+
|
|
37
|
+
```clojure
|
|
38
|
+
{:entries [{:name "place"
|
|
39
|
+
:namespace "demo.Board"
|
|
40
|
+
:complexity 3
|
|
41
|
+
:coverage 75.0
|
|
42
|
+
:crap 3.1}]}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
uml-viewer groups entries by `:namespace` and joins each operation on `:name`. `nil` coverage is what `--no-coverage` writes: complexity only, and CRAP is `nil` too. A function the coverage report does not mention scores 0%.
|
|
46
|
+
|
|
47
|
+
| Language | `:namespace` | `:name` |
|
|
48
|
+
| --- | --- | --- |
|
|
49
|
+
| Clojure | the `ns` | the `defn` / `defn-` name |
|
|
50
|
+
| Java | `package.Class`, or `package.Outer.Inner` | the method name |
|
|
51
|
+
| Go | the package import path, or `import/path.Receiver` | the function or method name |
|
|
52
|
+
| TypeScript | the dotted module path, or `module.Class` | the function or method name |
|
|
53
|
+
| Rust | `crate::module`, or `crate::module::Type` | the function or method name |
|
|
54
|
+
| Python | the dotted module path, or `module.Class` | the function or method name |
|
|
55
|
+
|
|
56
|
+
Rename or move is a new entry. There is no identity matching across runs.
|
|
57
|
+
|
|
58
|
+
## What each language counts
|
|
59
|
+
|
|
60
|
+
Clojure follows crap4clj: `if` / `when` and their variants, `and`, `or`, `loop`, `catch`, and each clause of `cond`, `condp`, `case`, `cond->`, `cond->>`, `some->`, and `some->>`. Coverage prefers Cloverage's per-line form counts and falls back to LCOV.
|
|
61
|
+
|
|
62
|
+
Java follows crap4java: `if`, loops, `catch`, `?:`, each `switch` label, and `&&` / `||`. Constructors and methods inside anonymous classes are omitted. Coverage is JaCoCo's `INSTRUCTION` counter.
|
|
63
|
+
|
|
64
|
+
Go follows crap4go: `if`, `for`, `range`, each `switch` and `select` clause, and `&&` / `||`. Coverage is `go test -coverprofile`, which names each file by its module import path (the `module` line of the nearest `go.mod` plus the path inside that module). crapper and mutator look a file up by that name first, then by the longest matching path suffix.
|
|
65
|
+
|
|
66
|
+
TypeScript, TSX, and JavaScript (`.js`, `.jsx`, `.mjs`, `.cjs`) use the same structural decisions as Java, including `&&` inside JSX, plus `??` and `?.`. Top-level functions, class methods, and top-level arrow functions are entries. An inline Express callback — `.get`, `.post`, `.put`, `.patch`, `.delete`, `.head`, `.options`, `.all`, `.use`, and `.route(path).get(...)` — is its own entry, named `GET /users`. A second callback on that same route is `GET /users#2`. Its decisions are not also charged to the enclosing function. Other nested callbacks stay inside the enclosing function. Coverage is LCOV. When the report has branch records (`BRDA`) inside a function, the score uses those branches; a function with no branches uses line hits. A `coverage` script is used as-is. A Vitest project runs `vitest --coverage` (installing `@vitest/coverage-v8` into `node_modules` when it is missing, without editing `package.json`). Other test scripts are wrapped in `c8`.
|
|
67
|
+
|
|
68
|
+
Rust counts `if`, loops, each `match` arm, `?`, and `&&` / `||`. `mod tests` is skipped. Coverage is LCOV from `cargo llvm-cov` or `cargo tarpaulin`, run in the nearest directory that contains `Cargo.toml`. When neither tool is installed, the run installs `cargo-llvm-cov`.
|
|
69
|
+
|
|
70
|
+
Python counts `if`, `elif`, `for`, `while`, `except`, each `match` case, a comprehension filter, a conditional expression, and each `and` / `or`. Nested functions stay inside the enclosing function. Coverage is LCOV from `coverage.py`, running pytest when the project uses it and `unittest` otherwise. `coverage` and `pytest` are installed into the project's interpreter when they are missing.
|
|
71
|
+
|
|
72
|
+
## Coverage commands
|
|
73
|
+
|
|
74
|
+
By default a run deletes the previous report for each language it is about to measure and regenerates it:
|
|
75
|
+
|
|
76
|
+
| Language | Command | Report |
|
|
77
|
+
| --- | --- | --- |
|
|
78
|
+
| Clojure | `clj -M:cov --lcov` (needs `deps.edn` or `bb.edn`) | `target/coverage/` |
|
|
79
|
+
| Java | JaCoCo Maven plugin `0.8.12` in each module with `pom.xml` | `target/site/jacoco/jacoco.xml` |
|
|
80
|
+
| Go | `go test ./... -coverprofile=...` | `target/coverage/go/coverage.out` |
|
|
81
|
+
| TypeScript | `npm run coverage`, or Vitest `--coverage`, or `npx c8 ... npm test` | `coverage/lcov.info` or `target/coverage/typescript/lcov.info` |
|
|
82
|
+
| Rust | `cargo llvm-cov` or `cargo tarpaulin`, per Cargo package | `target/coverage/rust/lcov.info` |
|
|
83
|
+
| Python | `coverage run` with pytest or unittest, then `coverage lcov` | `target/coverage/python/lcov.info` |
|
|
84
|
+
|
|
85
|
+
A missing tool or a failed test run scores that language at 0% and still writes the snapshot. Pass `--coverage-command` to replace those defaults with one command of your own. `--no-coverage` is the run that leaves coverage and CRAP as N/A.
|
|
86
|
+
|
|
87
|
+
## Development
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
python3 -m venv .venv
|
|
91
|
+
.venv/bin/pip install -e '.[dev]'
|
|
92
|
+
.venv/bin/pytest
|
|
93
|
+
```
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "crapper"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "CRAP scores for Clojure, Java, Go, TypeScript, Rust, and Python, in the snapshot uml-viewer reads."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = { file = "LICENSE" }
|
|
7
|
+
classifiers = ["License :: OSI Approved :: Zero-Clause BSD (0BSD)"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"tree-sitter>=0.24",
|
|
11
|
+
"tree-sitter-language-pack>=0.7",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
crapper = "crapper.cli:main"
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = ["pytest>=8"]
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["setuptools>=68"]
|
|
22
|
+
build-backend = "setuptools.build_meta"
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
|
26
|
+
|
|
27
|
+
[tool.pytest.ini_options]
|
|
28
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Turn source files and coverage into CRAP entries."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from crapper.coverage import CoverageBundle
|
|
6
|
+
from crapper.crap import make_entry, sort_entries
|
|
7
|
+
from crapper.discover import language_of
|
|
8
|
+
from crapper.languages import functions_in_file
|
|
9
|
+
from crapper.model import Entry, Function
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _coverage(bundle: CoverageBundle | None, function: Function) -> float | None:
|
|
13
|
+
"""Percentage for one function.
|
|
14
|
+
|
|
15
|
+
None means coverage was not requested (`--no-coverage`). A function the
|
|
16
|
+
report does not mention scores 0%, so it sorts with the other scores
|
|
17
|
+
instead of sinking to the bottom as N/A.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
if bundle is None:
|
|
21
|
+
return None
|
|
22
|
+
found = bundle.percent_for(function)
|
|
23
|
+
if found is None:
|
|
24
|
+
return 0.0
|
|
25
|
+
return found
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _source_path(file: Path, root: Path) -> str:
|
|
29
|
+
try:
|
|
30
|
+
return file.relative_to(root).as_posix()
|
|
31
|
+
except ValueError:
|
|
32
|
+
return file.as_posix()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def analyze_files(
|
|
36
|
+
files: list[Path],
|
|
37
|
+
project_root: Path,
|
|
38
|
+
bundle: CoverageBundle | None,
|
|
39
|
+
) -> list[Entry]:
|
|
40
|
+
root = project_root.resolve()
|
|
41
|
+
if bundle is not None:
|
|
42
|
+
bundle.bind_sources([_source_path(file.resolve(), root) for file in files])
|
|
43
|
+
entries: list[Entry] = []
|
|
44
|
+
for file in files:
|
|
45
|
+
file = file.resolve()
|
|
46
|
+
language = language_of(file)
|
|
47
|
+
if language is None:
|
|
48
|
+
continue
|
|
49
|
+
source = file.read_text(encoding="utf-8", errors="replace")
|
|
50
|
+
try:
|
|
51
|
+
relative = file.relative_to(root).as_posix()
|
|
52
|
+
except ValueError:
|
|
53
|
+
relative = file.as_posix()
|
|
54
|
+
for function in functions_in_file(language, source, relative, str(root)):
|
|
55
|
+
entries.append(make_entry(function, _coverage(bundle, function)))
|
|
56
|
+
return sort_entries(entries)
|