Skip to content

Commit 4197044

Browse files
committed
allow try to specify tests, and add no-ci-tests label to skip tests
1 parent 8f6d4a2 commit 4197044

File tree

3 files changed

+105
-12
lines changed

3 files changed

+105
-12
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ jobs:
110110
runs-on: ubuntu-latest
111111
outputs:
112112
matrix: ${{ steps.generate-matrix.outputs.matrix }}
113+
tests: ${{ steps.generate-matrix.outputs.tests }}
113114
steps:
114115
- uses: actions/checkout@v3
115116
with:
@@ -255,7 +256,8 @@ jobs:
255256

256257
# we should always have an artifact from a previous build.
257258
remote:
258-
needs: [test, check]
259+
needs: [test, check, generate-matrix]
260+
if: needs.generate-matrix.outputs.tests.remote
259261
runs-on: ubuntu-latest
260262
steps:
261263
- uses: actions/checkout@v3
@@ -275,7 +277,8 @@ jobs:
275277
shell: bash
276278

277279
bisect:
278-
needs: [test, check]
280+
needs: [test, check, generate-matrix]
281+
if: needs.generate-matrix.outputs.tests.bisect
279282
runs-on: ubuntu-latest
280283
steps:
281284
- uses: actions/checkout@v3
@@ -295,7 +298,8 @@ jobs:
295298
shell: bash
296299

297300
foreign:
298-
needs: [test, check]
301+
needs: [test, check, generate-matrix]
302+
if: needs.generate-matrix.outputs.tests.foreign
299303
runs-on: ubuntu-latest
300304
steps:
301305
- uses: actions/checkout@v3
@@ -321,7 +325,8 @@ jobs:
321325
shell: bash
322326

323327
docker-in-docker:
324-
needs: [test, check]
328+
needs: [test, check, generate-matrix]
329+
if: needs.generate-matrix.outputs.tests.docker-in-docker
325330
runs-on: ubuntu-latest
326331
steps:
327332
- uses: actions/checkout@v3
@@ -344,7 +349,8 @@ jobs:
344349
podman:
345350
name: podman
346351
runs-on: ubuntu-latest
347-
needs: [shellcheck, test, check]
352+
needs: [shellcheck, test, check, generate-matrix]
353+
if: needs.generate-matrix.outputs.tests.podman
348354
strategy:
349355
fail-fast: false
350356
outputs:

xtask/src/ci/target_matrix.rs

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::process::Command;
22

3-
use clap::builder::BoolishValueParser;
3+
use clap::builder::{BoolishValueParser, PossibleValuesParser};
44
use clap::Parser;
55
use cross::{shell::Verbosity, CommandExt};
66
use serde::{Deserialize, Serialize};
@@ -61,6 +61,7 @@ impl TargetMatrix {
6161
none: false,
6262
has_image: true,
6363
verbose: false,
64+
tests: vec!["all".to_owned()],
6465
},
6566
),
6667
TargetMatrix {
@@ -83,7 +84,7 @@ impl TargetMatrix {
8384
}
8485
) || is_default_try
8586
{
86-
apply_ci_target_labels(&prs, &mut app)?
87+
apply_ci_labels(&prs, &mut app)?
8788
}
8889

8990
app.filter(&mut matrix);
@@ -108,13 +109,15 @@ impl TargetMatrix {
108109
.collect::<Vec<_>>();
109110

110111
let json = serde_json::to_string(&matrix)?;
111-
gha_print(&json);
112112
gha_output("matrix", &json)?;
113+
let tests = serde_json::to_string(&app.tests()?)?;
114+
gha_output("tests", &tests)?;
113115
Ok(())
114116
}
115117
}
116118

117-
fn apply_ci_target_labels(prs: &[&str], app: &mut TargetMatrixArgs) -> Result<(), eyre::Error> {
119+
fn apply_ci_labels(prs: &[&str], app: &mut TargetMatrixArgs) -> Result<(), eyre::Error> {
120+
apply_has_no_ci_tests(prs, app)?;
118121
apply_has_no_ci_target(prs, app)?;
119122

120123
let mut to_add = vec![];
@@ -135,10 +138,22 @@ fn apply_ci_target_labels(prs: &[&str], app: &mut TargetMatrixArgs) -> Result<()
135138
Ok(())
136139
}
137140

141+
fn apply_has_no_ci_tests(prs: &[&str], app: &mut TargetMatrixArgs) -> Result<(), eyre::Error> {
142+
if !prs.is_empty()
143+
&& prs.iter().try_fold(true, |b, pr| {
144+
Ok::<_, eyre::Report>(b && has_no_ci_tests_label(pr)?)
145+
})?
146+
{
147+
app.none = true;
148+
app.tests.push("none".to_owned());
149+
}
150+
Ok(())
151+
}
152+
138153
fn apply_has_no_ci_target(prs: &[&str], app: &mut TargetMatrixArgs) -> Result<(), eyre::Error> {
139154
if !prs.is_empty()
140155
&& prs.iter().try_fold(true, |b, pr| {
141-
Ok::<_, eyre::Report>(b && has_no_ci_target(pr)?)
156+
Ok::<_, eyre::Report>(b && has_no_ci_target_label(pr)?)
142157
})?
143158
{
144159
app.none = true;
@@ -168,10 +183,14 @@ fn parse_gh_labels(pr: &str) -> cross::Result<Vec<String>> {
168183
Ok(pr_info.labels.into_iter().map(|l| l.name).collect())
169184
}
170185

171-
fn has_no_ci_target(pr: &str) -> cross::Result<bool> {
186+
fn has_no_ci_target_label(pr: &str) -> cross::Result<bool> {
172187
Ok(parse_gh_labels(pr)?.contains(&"no-ci-targets".to_owned()))
173188
}
174189

190+
fn has_no_ci_tests_label(pr: &str) -> cross::Result<bool> {
191+
Ok(parse_gh_labels(pr)?.contains(&"no-ci-tests".to_owned()))
192+
}
193+
175194
/// Convert a `GITHUB_REF` into it's merge group pr
176195
fn process_merge_group(ref_: &str) -> cross::Result<&str> {
177196
ref_.split('/')
@@ -227,7 +246,7 @@ struct TargetMatrixElement<'a> {
227246
verbose: bool,
228247
}
229248

230-
#[derive(Parser, Debug, Default, PartialEq, Eq)]
249+
#[derive(Parser, Debug, PartialEq, Eq)]
231250
#[clap(no_binary_name = true)]
232251
struct TargetMatrixArgs {
233252
#[clap(long, short, num_args = 0..)]
@@ -248,6 +267,37 @@ struct TargetMatrixArgs {
248267
has_image: bool,
249268
#[clap(long, short)]
250269
verbose: bool,
270+
#[clap(long, value_parser = PossibleValuesParser::new(&[
271+
"remote",
272+
"bisect",
273+
"foreign",
274+
"docker-in-docker",
275+
"podman",
276+
"none",
277+
"all"
278+
]),
279+
num_args = 0..,
280+
value_delimiter = ',',
281+
default_value = "all"
282+
)]
283+
tests: Vec<String>,
284+
}
285+
286+
impl Default for TargetMatrixArgs {
287+
fn default() -> Self {
288+
Self {
289+
target: Vec::new(),
290+
std: None,
291+
cpp: None,
292+
dylib: None,
293+
run: None,
294+
runners: Vec::new(),
295+
none: false,
296+
has_image: false,
297+
verbose: false,
298+
tests: vec!["all".to_owned()],
299+
}
300+
}
251301
}
252302

253303
impl TargetMatrixArgs {
@@ -298,6 +348,42 @@ impl TargetMatrixArgs {
298348
});
299349
}
300350
}
351+
352+
fn tests(&self) -> Result<serde_json::Value, serde_json::Error> {
353+
use clap::CommandFactory;
354+
use serde::ser::SerializeMap;
355+
struct Ser(Vec<String>);
356+
impl serde::Serialize for Ser {
357+
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
358+
let mut map = serializer.serialize_map(Some(self.0.len()))?;
359+
for e in &self.0 {
360+
map.serialize_entry(&e, &true)?;
361+
}
362+
map.end()
363+
}
364+
}
365+
let tests = match (
366+
self.tests.iter().any(|t| t == "all"),
367+
self.tests.iter().any(|t| t == "none"),
368+
) {
369+
(_, true) => vec![],
370+
(true, false) => {
371+
let mut possible: Vec<String> = Self::command()
372+
.get_arguments()
373+
.find(|arg| arg.get_id() == "tests")
374+
.expect("a `tests` argument should exist")
375+
.get_possible_values()
376+
.into_iter()
377+
.map(|p| p.get_name().to_owned())
378+
.collect();
379+
possible.retain(|p| p != "all");
380+
possible.retain(|p| p != "none");
381+
possible
382+
}
383+
_ => self.tests.clone(),
384+
};
385+
serde_json::to_value(Ser(tests))
386+
}
301387
}
302388

303389
#[cfg(test)]

xtask/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ pub fn write_to_string(path: &Path, contents: &str) -> cross::Result<()> {
348348

349349
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files
350350
pub fn write_to_gha_env_file(env_name: &str, contents: &str) -> cross::Result<()> {
351+
eprintln!("{contents}");
351352
let path = if let Ok(path) = env::var(env_name) {
352353
PathBuf::from(path)
353354
} else {

0 commit comments

Comments
 (0)