Skip to content

Commit 28638c7

Browse files
committed
Add basic change detection framework
1 parent c5be671 commit 28638c7

2 files changed

Lines changed: 191 additions & 0 deletions

File tree

‎src/languages/changes.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import test from "ava";
2+
3+
import type { FileDiff } from "../diff-informed";
4+
import { setupTests } from "../testing-utils";
5+
6+
import { matchPatterns, relevantChangeFor } from "./changes";
7+
8+
setupTests(test);
9+
10+
function mkTestDiff(filename: string): FileDiff {
11+
return { filename, changes: 42 };
12+
}
13+
14+
test("matchPatterns - true if any pattern matches", (t) => {
15+
t.true(matchPatterns("/path/to/file.js", ["*.js"]));
16+
t.true(matchPatterns("/path/to", ["/path/**"]));
17+
t.true(matchPatterns("/path/to", ["foo.txt", "/path/**"]));
18+
t.true(matchPatterns("/path/to", ["foo.txt", "/path/from", "/path/to*"]));
19+
});
20+
21+
test("matchPatterns - false if no pattern matches", (t) => {
22+
t.false(matchPatterns("/path/to", []));
23+
t.false(matchPatterns("/path/to", ["/some/path/**"]));
24+
});
25+
26+
test("relevantChangeFor - relevant if expected", (t) => {
27+
// No configuration: all files are relevant
28+
t.true(relevantChangeFor({}, mkTestDiff("/path/to/file.js")));
29+
30+
// Various configurations that we expect to match the given path.
31+
t.true(
32+
relevantChangeFor({ patterns: ["*.js"] }, mkTestDiff("/path/to/file.js")),
33+
);
34+
t.true(
35+
relevantChangeFor(
36+
{ patterns: ["/path/**"] },
37+
mkTestDiff("/path/to/file.js"),
38+
),
39+
);
40+
41+
// `exclude` configuration doesn't apply
42+
t.true(
43+
relevantChangeFor({ exclude: ["*.ts"] }, mkTestDiff("/path/to/file.js")),
44+
);
45+
t.true(
46+
relevantChangeFor(
47+
{ patterns: ["*.js"], exclude: ["file.ts"] },
48+
mkTestDiff("/path/to/file.js"),
49+
),
50+
);
51+
t.true(
52+
relevantChangeFor(
53+
{ patterns: ["/path/**"], exclude: ["/path/to/**/*.ts"] },
54+
mkTestDiff("/path/to/file.js"),
55+
),
56+
);
57+
});
58+
59+
test("relevantChangeFor - exclude patterns filter results", (t) => {
60+
// No patterns: all files are relevant, but `exclude` filters `.js` files.
61+
t.false(
62+
relevantChangeFor({ exclude: ["*.js"] }, mkTestDiff("/path/to/file.js")),
63+
);
64+
65+
// Various `patterns` configurations that we expect to match the given path and
66+
// more specific `exclude` configurations that we expect to also match.
67+
t.false(
68+
relevantChangeFor(
69+
{ patterns: ["*.js"], exclude: ["file.js"] },
70+
mkTestDiff("/path/to/file.js"),
71+
),
72+
);
73+
t.false(
74+
relevantChangeFor(
75+
{ patterns: ["/path/**"], exclude: ["/path/to/**/*.js"] },
76+
mkTestDiff("/path/to/file.js"),
77+
),
78+
);
79+
});

‎src/languages/changes.ts‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { minimatch } from "minimatch";
2+
3+
import type { FileDiff } from "../diff-informed";
4+
5+
import { BuiltInLanguage } from ".";
6+
7+
/**
8+
* Represents configurations for how to detect whether changes relate to a given analysis.
9+
*/
10+
export type ChangePatterns = {
11+
/**
12+
* A list of glob patterns.
13+
*
14+
* If a changed file has matches one of these, we conclude that files relevant to the
15+
* analysis have changed.
16+
*
17+
* If undefined, all changes are considered relevant.
18+
*/
19+
patterns?: string[];
20+
21+
/**
22+
* A list of glob patterns to exclude from `patterns`. Allows specific patterns to be excluded
23+
* that would otherwise be included by `patterns`.
24+
*
25+
* If a changed file matches one of `patterns` and `exclude`, then it is not considered
26+
* relevant to the analysis.
27+
*/
28+
exclude?: string[];
29+
};
30+
31+
/**
32+
* A mapping from all {@link BuildInLanguage}s to a {@link ChangePatterns} configuration.
33+
*/
34+
export const CHANGE_PATTERNS: Record<BuiltInLanguage, ChangePatterns> = {
35+
[BuiltInLanguage.actions]: {},
36+
[BuiltInLanguage.cpp]: {},
37+
[BuiltInLanguage.csharp]: {},
38+
[BuiltInLanguage.go]: {},
39+
[BuiltInLanguage.java]: {},
40+
[BuiltInLanguage.javascript]: {},
41+
[BuiltInLanguage.python]: {},
42+
[BuiltInLanguage.ruby]: {},
43+
[BuiltInLanguage.rust]: {},
44+
[BuiltInLanguage.swift]: {},
45+
};
46+
47+
/**
48+
* Decides whether {@link val} matches any of {@link patterns}. Returns early
49+
* as soon as a pattern matches.
50+
*/
51+
export function matchPatterns(val: string, patterns: string[]): boolean {
52+
for (const pattern of patterns) {
53+
if (minimatch(val, pattern, { matchBase: true })) {
54+
return true;
55+
}
56+
}
57+
58+
return false;
59+
}
60+
61+
/**
62+
* Decides whether {@link diff} matches the configuration in {@link changePatterns}.
63+
*
64+
* @param changePatterns The configuration of patterns to match {@link diff}'s path against.
65+
* @param diff A description of a change.
66+
*/
67+
export function relevantChangeFor(
68+
changePatterns: ChangePatterns,
69+
diff: FileDiff,
70+
): boolean {
71+
// If `patterns` is defined and none match, then the change is not relevant.
72+
if (
73+
changePatterns.patterns !== undefined &&
74+
!matchPatterns(diff.filename, changePatterns.patterns)
75+
) {
76+
return false;
77+
}
78+
79+
// At this point we have decided that the change is relevant, either because no
80+
// patterns are configured (all changes are relevant) or because one of the configured
81+
// patterns matched. Let's check if `exclude` patterns are configured and if any match.
82+
// If so, then we don't consider the change relevant after all.
83+
if (
84+
changePatterns.exclude !== undefined &&
85+
matchPatterns(diff.filename, changePatterns.exclude)
86+
) {
87+
return false;
88+
}
89+
90+
// The change is relevant and none of the `exclude` patterns matched (or none were configured).
91+
return true;
92+
}
93+
94+
/**
95+
* Decides whether at least one of the given {@link diffs} matches the configuration for {@link language}
96+
* from {@link CHANGE_PATTERNS}.
97+
*
98+
* @param language The language to check for changes for.
99+
* @param diffs All changes to check.
100+
*/
101+
export function relevantChangesFor(
102+
language: BuiltInLanguage,
103+
diffs: FileDiff[],
104+
): boolean {
105+
for (const diff of diffs) {
106+
if (relevantChangeFor(CHANGE_PATTERNS[language], diff)) {
107+
return true;
108+
}
109+
}
110+
111+
return false;
112+
}

0 commit comments

Comments
 (0)