Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make prettier a Rust test #889

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:

- name: Install tools
run: |
# smoelius: Prettier is still needed for scripts/update_example_READMEs.sh.
npm install -g prettier
rm -f "$HOME"/.cargo/bin/cargo-fmt
rm -f "$HOME"/.cargo/bin/rustfmt
Expand All @@ -61,9 +62,6 @@ jobs:
cargo install cargo-rdme || true
cargo install cargo-sort || true

- name: Prettier
run: ./scripts/check_prettier.sh

- name: Cargo sort
run: find . -name Cargo.toml -print0 | xargs -0 -n 1 dirname | xargs -n 1 cargo sort --check --grouped

Expand Down Expand Up @@ -130,6 +128,7 @@ jobs:
# smoelius: This list will grow: https://github.com/trailofbits/dylint/issues/636
- name: Install tools
run: |
npm install -g prettier
cargo install cargo-hack || true
cargo install cargo-msrv || true
cargo install cargo-supply-chain || true
Expand Down
76 changes: 76 additions & 0 deletions cargo-dylint/tests/dylint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use std::{
env::{set_current_dir, set_var},
ffi::OsStr,
fs::{read_to_string, write},
io::{stderr, Write},
path::Path,
str::FromStr,
sync::Mutex,
};
use tempfile::tempdir;

Expand Down Expand Up @@ -294,6 +296,32 @@ fn msrv() {
}
}

#[cfg(not(windows))]
#[test]
fn prettier_all_but_examples_and_template() {
Command::new("prettier")
.args([
"--check",
"--ignore-path",
"cargo-dylint/tests/prettier_ignore.txt",
"**/*.md",
"**/*.yml",
])
.assert()
.success();
}

#[cfg(not(windows))]
#[test]
fn prettier_examples_and_template() {
preserves_cleanliness("prettier", true, || {
Command::new("prettier")
.args(["--write", "examples/**/*.md", "internal/template/**/*.md"])
.assert()
.success();
});
}

// smoelius: `supply_chain` is the only test that uses `supply_chain.json`. So there is no race.
#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]
#[cfg_attr(dylint_lib = "overscoped_allow", allow(overscoped_allow))]
Expand Down Expand Up @@ -351,3 +379,51 @@ fn walkdir(include_examples: bool) -> impl Iterator<Item = walkdir::Result<walkd
&& (include_examples || entry.path().file_name() != Some(OsStr::new("examples")))
})
}

static MUTEX: Mutex<()> = Mutex::new(());

fn preserves_cleanliness(test_name: &str, ignore_blank_lines: bool, f: impl FnOnce()) {
let _lock = MUTEX.lock().unwrap();

if cfg!(not(feature = "strict")) && dirty(false).is_some() {
#[allow(clippy::explicit_write)]
writeln!(
stderr(),
"Skipping `{test_name}` test as repository is dirty"
)
.unwrap();
return;
}

f();

if let Some(stdout) = dirty(ignore_blank_lines) {
panic!("{}", stdout);
}

// smoelius: If the repository is not dirty with `ignore_blank_lines` set to true, but would be
// dirty otherwise, then restore the repository's contents.
if ignore_blank_lines && dirty(false).is_some() {
Command::new("git")
.args(["checkout", "."])
.assert()
.success();
}
}

fn dirty(ignore_blank_lines: bool) -> Option<String> {
let mut command = Command::new("git");
command.arg("diff");
if ignore_blank_lines {
command.arg("--ignore-blank-lines");
}
let output = command.output().unwrap();

// smoelius: `--ignore-blank-lines` does not work with `--exit-code`. So instead check whether
// stdout is empty.
if output.stdout.is_empty() {
None
} else {
Some(String::from_utf8(output.stdout).unwrap())
}
}
2 changes: 2 additions & 0 deletions cargo-dylint/tests/prettier_ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
examples
template
27 changes: 0 additions & 27 deletions scripts/check_prettier.sh

This file was deleted.

16 changes: 0 additions & 16 deletions scripts/unquote_yaml_strings.sh

This file was deleted.