Skip to content

Commit 940668b

Browse files
committed
run package tests
1 parent 48904f7 commit 940668b

File tree

8 files changed

+110
-55
lines changed

8 files changed

+110
-55
lines changed

.github/testing/.eslintrc.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
{
2-
"extends": "./node_modules/gts/"
2+
"extends": "./node_modules/gts/",
3+
"rules": {
4+
"@typescript-eslint/no-unused-vars": [
5+
"warn",
6+
{
7+
"argsIgnorePattern": "^_",
8+
"varsIgnorePattern": "^_",
9+
"caughtErrorsIgnorePattern": "^_"
10+
}
11+
]
12+
}
313
}

.github/testing/src/affected.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,42 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Map, List, Set } from 'immutable';
15+
import {Map, List, Set} from 'immutable';
1616

1717
export type PackageName = string;
1818
export type TestPath = string;
1919
export type TestName = string;
2020

21-
export const TestAll = (path: string): Affected => ({ path: path, TestAll: null });
22-
export const TestSome = (path: string, tests: Map<TestPath, Set<TestName>>): Affected => ({
21+
export const TestAll = (path: string): Affected => ({
22+
path: path,
23+
TestAll: null,
24+
});
25+
export const TestSome = (
26+
path: string,
27+
tests: Map<TestPath, Set<TestName>>
28+
): Affected => ({
2329
path: path,
2430
TestSome: tests,
2531
});
2632
export type Affected =
27-
| { path: string, TestAll: null }
28-
| { path: string, TestSome: Map<TestPath, Set<TestName>> };
33+
| {path: string; TestAll: null}
34+
| {path: string; TestSome: Map<TestPath, Set<TestName>>};
2935

30-
export function mergeAffected(path: string, affected: List<Affected>): Affected {
31-
return affected.reduce((result, current) => {
32-
if ('TestSome' in result && 'TestSome' in current) {
33-
const tests = result.TestSome.mergeWith((xs, ys) => xs.union(ys), current.TestSome)
34-
return TestSome(path, tests);
35-
}
36-
return TestAll(path);
37-
}, TestSome(path, Map()));
36+
export function mergeAffected(
37+
path: string,
38+
affected: List<Affected>
39+
): Affected {
40+
return affected.reduce(
41+
(result, current) => {
42+
if ('TestSome' in result && 'TestSome' in current) {
43+
const tests = result.TestSome.mergeWith(
44+
(xs, ys) => xs.union(ys),
45+
current.TestSome
46+
);
47+
return TestSome(path, tests);
48+
}
49+
return TestAll(path);
50+
},
51+
TestSome(path, Map())
52+
);
3853
}

.github/testing/src/config.ts

+22-25
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,16 @@
1515
import * as fs from 'node:fs';
1616
import * as git from './git';
1717
import * as path from 'path';
18-
import { List, Map, Set } from 'immutable';
19-
import { minimatch } from 'minimatch'; /* eslint-disable @typescript-eslint/no-explicit-any */
20-
import {
21-
Affected,
22-
PackageName,
23-
TestAll,
24-
TestName,
25-
TestPath,
26-
mergeAffected,
27-
} from './affected';
18+
import {List, Map, Set} from 'immutable';
19+
import {minimatch} from 'minimatch'; /* eslint-disable @typescript-eslint/no-explicit-any */
20+
import {Affected, TestAll, TestName, TestPath, mergeAffected} from './affected';
2821

2922
export class Config {
3023
match: List<string>;
3124
ignore: List<string>;
3225
packageFile: List<string>;
33-
testAll: (path: PackageName) => void;
34-
testSome: (path: PackageName, tests: Map<TestPath, Set<TestName>>) => void;
26+
testAll: () => void;
27+
testSome: (tests: Map<TestPath, Set<TestName>>) => void;
3528

3629
constructor({
3730
match,
@@ -43,14 +36,16 @@ export class Config {
4336
match?: string[];
4437
ignore?: string[];
4538
packageFile?: string[];
46-
testAll?: (path: PackageName) => void;
47-
testSome?: (path: PackageName, tests: Map<TestPath, Set<TestName>>) => void;
39+
testAll?: () => void;
40+
testSome?: (tests: Map<TestPath, Set<TestName>>) => void;
4841
}) {
4942
this.match = List(match || ['**']);
5043
this.ignore = List(ignore);
5144
this.packageFile = List(packageFile);
52-
this.testAll = testAll || (path => { });
53-
this.testSome = testSome || ((path, tests) => { });
45+
this.testAll = testAll || (() => {});
46+
this.testSome =
47+
testSome ||
48+
(_ => {}); /* eslint-disable @typescript-eslint/no-unused-vars */
5449
}
5550

5651
affected = (head: string, main: string): List<Affected> =>
@@ -65,25 +60,27 @@ export class Config {
6560
);
6661

6762
test = (affected: Affected) => {
68-
const cwd = process.cwd()
69-
process.chdir(git.root())
63+
const cwd = process.cwd();
64+
const dir = path.join(git.root(), affected.path);
65+
console.log(`>> cd ${dir}`);
66+
process.chdir(dir);
7067
if ('TestAll' in affected) {
71-
this.testAll(affected.path)
68+
this.testAll();
7269
}
7370
if ('TestSome' in affected) {
74-
this.testSome(affected.path, affected.TestSome)
71+
this.testSome(affected.TestSome);
7572
}
76-
process.chdir(cwd)
77-
}
73+
process.chdir(cwd);
74+
};
7875

7976
matchFile = (diff: git.Diff): boolean =>
8077
this.match.some(p => minimatch(diff.filename, p)) &&
8178
this.ignore.some(p => !minimatch(diff.filename, p));
8279

8380
findAffected = (diff: git.Diff): Affected => {
84-
const path = this.findPackage(diff.filename)
85-
return TestAll(path) // TOOD: discover affected tests only
86-
}
81+
const path = this.findPackage(diff.filename);
82+
return TestAll(path); // TOOD: discover affected tests only
83+
};
8784

8885
findPackage = (filename: string): string => {
8986
const dir = path.dirname(filename);

.github/testing/src/config/python.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Config } from '../config';
15+
import * as subprocess from '../subprocess';
16+
import {Config} from '../config';
1617

1718
export const python = new Config({
1819
match: ['**'],
@@ -24,10 +25,10 @@ export const python = new Config({
2425
'setup.py',
2526
'setup.cfg',
2627
],
27-
testAll: path => {
28-
throw `TODO: config/python.ts testAll ${path}`
28+
testAll: () => {
29+
subprocess.run('nox', ['-s', 'py-3.11']);
30+
},
31+
testSome: tests => {
32+
throw `TODO: config/python.ts testSome ${JSON.stringify(tests)}`;
2933
},
30-
testSome: (path, tests) => {
31-
throw `TODO: config/python.ts testSome ${path} ${JSON.stringify(tests)}`
32-
}
3334
});

.github/testing/src/git.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,30 @@
1313
// limitations under the License.
1414

1515
import { List } from 'immutable';
16-
import { spawnSync } from 'child_process';
16+
import * as subprocess from './subprocess';
1717

1818
export type Diff = {
1919
filename: string;
2020
lineNumbers: number[];
2121
};
2222

2323
export function branchName(): string {
24-
const p = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
25-
return p.stdout.toString().trim();
24+
return subprocess.output('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
2625
}
2726

2827
export function root(): string {
29-
const p = spawnSync('git', ['rev-parse', '--show-toplevel']);
30-
return p.stdout.toString().trim();
28+
return subprocess.output('git', ['rev-parse', '--show-toplevel']);
3129
}
3230

3331
export function diffs(commit1: string, commit2: string): List<Diff> {
34-
const p = spawnSync('git', [
32+
const output = subprocess.output('git', [
3533
'--no-pager',
3634
'diff',
3735
'--unified=0',
3836
commit1,
3937
commit2,
4038
]);
41-
return List(p.stdout.toString().split(/^diff --git a\//m))
39+
return List(output.split(/^diff --git a\//m))
4240
.map(output => output.split('\n').filter(line => line.length > 0))
4341
.filter(lines => lines.length > 0)
4442
.map(lines => ({

.github/testing/src/main.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// limitations under the License.
1414

1515
import * as git from './git';
16-
import { Config } from './config';
17-
import { python } from './config/python';
18-
import { Affected } from './affected';
16+
import {Config} from './config';
17+
import {python} from './config/python';
18+
import {Affected} from './affected';
1919

2020
function getConfig(lang: string): Config {
2121
switch (lang) {

.github/testing/src/subprocess.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { spawnSync } from 'child_process';
16+
17+
export function run(cmd: string, args: string[]) {
18+
const p = spawnSync(cmd, args, { stdio: 'inherit' });
19+
process.exitCode = p.status || undefined;
20+
}
21+
22+
export function output(cmd: string, args: string[]): string {
23+
const p = spawnSync(cmd, args);
24+
process.exitCode = p.status || undefined;
25+
return p.stdout.toString().trim();
26+
}
27+

.github/workflows/test.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ jobs:
5757
- uses: actions/setup-node@v4
5858
with:
5959
node-version: '20.x'
60+
- uses: actions/setup-python@v5
61+
with:
62+
python-version: '3.11'
63+
- run: python --version
64+
- run: pip --version
65+
- run: pip install --upgrade pip
66+
- run: pip install nox
6067
- run: npm ci
6168
- run: npm run build
6269
- run: npm run tests python "$AFFECTED"

0 commit comments

Comments
 (0)