Skip to content

Commit 3417a84

Browse files
committed
Move cli tests to cli directory
1 parent 3861bd9 commit 3417a84

File tree

7 files changed

+123
-117
lines changed

7 files changed

+123
-117
lines changed

.claude/settings.local.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(cargo test:*)",
5+
"Bash(cargo clippy:*)",
6+
"Bash(cargo fmt:*)",
7+
"Bash(cargo check:*)",
8+
"Bash(rustfmt:*)"
9+
],
10+
"deny": []
11+
}
12+
}

crates/ty/tests/config_option.rs renamed to crates/ty/tests/cli/config_option.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use insta_cmd::assert_cmd_snapshot;
22

3-
mod common;
4-
use common::CliTest;
3+
use crate::CliTest;
54

65
#[test]
76
fn cli_config_args_toml_string_basic() -> anyhow::Result<()> {

crates/ty/tests/exit_code.rs renamed to crates/ty/tests/cli/exit_code.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use insta_cmd::assert_cmd_snapshot;
22

3-
mod common;
4-
use common::CliTest;
3+
use crate::CliTest;
54

65
#[test]
76
fn only_warnings() -> anyhow::Result<()> {

crates/ty/tests/cli.rs renamed to crates/ty/tests/cli/main.rs

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
use insta_cmd::assert_cmd_snapshot;
2-
use std::fmt::Write;
3-
4-
mod common;
5-
use common::CliTest;
1+
mod config_option;
2+
mod exit_code;
3+
mod python_environment;
4+
mod rule_selection;
5+
6+
use anyhow::Context as _;
7+
use insta::internals::SettingsBindDropGuard;
8+
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
9+
use std::{
10+
fmt::Write,
11+
path::{Path, PathBuf},
12+
process::Command,
13+
};
14+
use tempfile::TempDir;
615

716
#[test]
817
fn test_run_in_sub_directory() -> anyhow::Result<()> {
@@ -586,3 +595,96 @@ fn can_handle_large_binop_expressions() -> anyhow::Result<()> {
586595

587596
Ok(())
588597
}
598+
599+
pub(crate) struct CliTest {
600+
_temp_dir: TempDir,
601+
_settings_scope: SettingsBindDropGuard,
602+
project_dir: PathBuf,
603+
}
604+
605+
impl CliTest {
606+
pub(crate) fn new() -> anyhow::Result<Self> {
607+
let temp_dir = TempDir::new()?;
608+
609+
// Canonicalize the tempdir path because macos uses symlinks for tempdirs
610+
// and that doesn't play well with our snapshot filtering.
611+
// Simplify with dunce because otherwise we get UNC paths on Windows.
612+
let project_dir = dunce::simplified(
613+
&temp_dir
614+
.path()
615+
.canonicalize()
616+
.context("Failed to canonicalize project path")?,
617+
)
618+
.to_path_buf();
619+
620+
let mut settings = insta::Settings::clone_current();
621+
settings.add_filter(&tempdir_filter(&project_dir), "<temp_dir>/");
622+
settings.add_filter(r#"\\(\w\w|\s|\.|")"#, "/$1");
623+
624+
let settings_scope = settings.bind_to_scope();
625+
626+
Ok(Self {
627+
project_dir,
628+
_temp_dir: temp_dir,
629+
_settings_scope: settings_scope,
630+
})
631+
}
632+
633+
pub(crate) fn with_files<'a>(
634+
files: impl IntoIterator<Item = (&'a str, &'a str)>,
635+
) -> anyhow::Result<Self> {
636+
let case = Self::new()?;
637+
case.write_files(files)?;
638+
Ok(case)
639+
}
640+
641+
pub(crate) fn with_file(path: impl AsRef<Path>, content: &str) -> anyhow::Result<Self> {
642+
let case = Self::new()?;
643+
case.write_file(path, content)?;
644+
Ok(case)
645+
}
646+
647+
pub(crate) fn write_files<'a>(
648+
&self,
649+
files: impl IntoIterator<Item = (&'a str, &'a str)>,
650+
) -> anyhow::Result<()> {
651+
for (path, content) in files {
652+
self.write_file(path, content)?;
653+
}
654+
655+
Ok(())
656+
}
657+
658+
pub(crate) fn write_file(&self, path: impl AsRef<Path>, content: &str) -> anyhow::Result<()> {
659+
let path = path.as_ref();
660+
let path = self.project_dir.join(path);
661+
662+
if let Some(parent) = path.parent() {
663+
std::fs::create_dir_all(parent)
664+
.with_context(|| format!("Failed to create directory `{}`", parent.display()))?;
665+
}
666+
std::fs::write(&path, &*ruff_python_trivia::textwrap::dedent(content))
667+
.with_context(|| format!("Failed to write file `{path}`", path = path.display()))?;
668+
669+
Ok(())
670+
}
671+
672+
pub(crate) fn root(&self) -> &Path {
673+
&self.project_dir
674+
}
675+
676+
pub(crate) fn command(&self) -> Command {
677+
let mut command = Command::new(get_cargo_bin("ty"));
678+
command.current_dir(&self.project_dir).arg("check");
679+
680+
// Unset environment variables that can affect test behavior
681+
command.env_remove("VIRTUAL_ENV");
682+
command.env_remove("CONDA_PREFIX");
683+
684+
command
685+
}
686+
}
687+
688+
fn tempdir_filter(path: &Path) -> String {
689+
format!(r"{}\\?/?", regex::escape(path.to_str().unwrap()))
690+
}

crates/ty/tests/python_environment.rs renamed to crates/ty/tests/cli/python_environment.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use insta_cmd::assert_cmd_snapshot;
22
use ruff_python_ast::PythonVersion;
33

4-
mod common;
5-
use common::CliTest;
4+
use crate::CliTest;
65

76
/// Specifying an option on the CLI should take precedence over the same setting in the
87
/// project's configuration. Here, this is tested for the Python version.

crates/ty/tests/rule_selection.rs renamed to crates/ty/tests/cli/rule_selection.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use insta_cmd::assert_cmd_snapshot;
22

3-
mod common;
4-
use common::CliTest;
3+
use crate::CliTest;
54

65
/// The rule severity can be changed in the configuration file
76
#[test]

crates/ty/tests/common/mod.rs

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)