Remove test-utilities crate (#892)

This commit is contained in:
Casey Rodarmor 2021-07-03 14:26:59 -07:00 committed by GitHub
parent a24c86ed5a
commit d797592365
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 96 additions and 258 deletions

19
Cargo.lock generated
View File

@ -216,7 +216,7 @@ dependencies = [
"strum_macros", "strum_macros",
"target", "target",
"tempfile", "tempfile",
"test-utilities", "temptree",
"unicode-width", "unicode-width",
"which", "which",
"yaml-rust", "yaml-rust",
@ -529,6 +529,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "temptree"
version = "0.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b41283c421539cd57fda2bdae139a0e08992dba973cd4ba859765c867ad591"
dependencies = [
"tempfile",
]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.2" version = "1.1.2"
@ -538,14 +547,6 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "test-utilities"
version = "0.0.0"
dependencies = [
"just",
"tempfile",
]
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"

View File

@ -46,12 +46,10 @@ features = ["derive"]
[dev-dependencies] [dev-dependencies]
executable-path = "1.0.0" executable-path = "1.0.0"
pretty_assertions = "0.7.0" pretty_assertions = "0.7.0"
temptree = "0.0.0"
which = "4.0.0" which = "4.0.0"
yaml-rust = "0.4.5" yaml-rust = "0.4.5"
[dev-dependencies.test-utilities]
path = "test-utilities"
[features] [features]
# No features are active by default. # No features are active by default.
default = [] default = []

View File

@ -197,7 +197,7 @@ impl Search {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use test_utilities::tmptree; use temptree::temptree;
#[test] #[test]
fn not_found() { fn not_found() {
@ -305,7 +305,7 @@ mod tests {
#[test] #[test]
fn justfile_symlink_parent() { fn justfile_symlink_parent() {
let tmp = tmptree! { let tmp = temptree! {
src: "", src: "",
sub: {}, sub: {},
}; };

View File

@ -31,7 +31,12 @@ pub(crate) fn search(config: &Config) -> Search {
} }
} }
pub(crate) use test_utilities::tempdir; pub(crate) fn tempdir() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("just-test-tempdir")
.tempdir()
.expect("failed to create temporary directory")
}
macro_rules! analysis_error { macro_rules! analysis_error {
( (

View File

@ -1,10 +0,0 @@
[package]
name = "test-utilities"
version = "0.0.0"
authors = ["Casey Rodarmor <casey@rodarmor.com>"]
edition = "2018"
publish = false
[dependencies]
tempfile = "3"
just = { path = ".." }

View File

@ -1,149 +0,0 @@
use std::{collections::HashMap, fs, path::Path, process::Output};
pub fn tempdir() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("just-test-tempdir")
.tempdir()
.expect("failed to create temporary directory")
}
pub fn assert_success(output: &Output) {
if !output.status.success() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
panic!("{}", output.status);
}
}
pub fn assert_stdout(output: &Output, stdout: &str) {
assert_success(output);
assert_eq!(String::from_utf8_lossy(&output.stdout), stdout);
}
pub enum Entry {
File {
contents: &'static str,
},
Dir {
entries: HashMap<&'static str, Entry>,
},
}
impl Entry {
fn instantiate(self, path: &Path) {
match self {
Entry::File { contents } => fs::write(path, contents).expect("Failed to write tempfile"),
Entry::Dir { entries } => {
fs::create_dir(path).expect("Failed to create tempdir");
for (name, entry) in entries {
entry.instantiate(&path.join(name));
}
},
}
}
pub fn instantiate_base(base: &Path, entries: HashMap<&'static str, Entry>) {
for (name, entry) in entries {
entry.instantiate(&base.join(name));
}
}
}
#[macro_export]
macro_rules! entry {
{
{
$($contents:tt)*
}
} => {
$crate::Entry::Dir{entries: $crate::entries!($($contents)*)}
};
{
$contents:expr
} => {
$crate::Entry::File{contents: $contents}
};
}
#[macro_export]
macro_rules! entries {
{
} => {
std::collections::HashMap::new()
};
{
$($name:tt : $contents:tt,)*
} => {
{
use std::collections::HashMap;
let mut entries: HashMap<&'static str, $crate::Entry> = HashMap::new();
$(
entries.insert($crate::name!($name), $crate::entry!($contents));
)*
entries
}
}
}
#[macro_export]
macro_rules! name {
{
$name:ident
} => {
stringify!($name)
};
{
$name:literal
} => {
$name
};
}
#[macro_export]
macro_rules! tmptree {
{
$($contents:tt)*
} => {
{
let tempdir = $crate::tempdir();
let entries = $crate::entries!($($contents)*);
$crate::Entry::instantiate_base(&tempdir.path(), entries);
tempdir
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tmptree_file() {
let tmpdir = tmptree! {
foo: "bar",
};
let contents = fs::read_to_string(tmpdir.path().join("foo")).unwrap();
assert_eq!(contents, "bar");
}
#[test]
fn tmptree_dir() {
let tmpdir = tmptree! {
foo: {
bar: "baz",
},
};
let contents = fs::read_to_string(tmpdir.path().join("foo/bar")).unwrap();
assert_eq!(contents, "baz");
}
}

6
tests/assert_stdout.rs Normal file
View File

@ -0,0 +1,6 @@
use crate::common::*;
pub(crate) fn assert_stdout(output: &Output, stdout: &str) {
assert_success(output);
assert_eq!(String::from_utf8_lossy(&output.stdout), stdout);
}

9
tests/assert_success.rs Normal file
View File

@ -0,0 +1,9 @@
use crate::common::*;
pub(crate) fn assert_success(output: &Output) {
if !output.status.success() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
panic!("{}", output.status);
}
}

View File

@ -115,7 +115,7 @@ test! {
#[test] #[test]
fn default() { fn default() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "foo:\n echo foo\n", justfile: "foo:\n echo foo\n",
}; };

View File

@ -1,17 +1,22 @@
pub(crate) use std::{ pub(crate) use std::{
collections::BTreeMap, collections::BTreeMap,
env::{self, consts::EXE_SUFFIX}, env::{self, consts::EXE_SUFFIX},
error::Error,
fs, fs,
io::Write, io::Write,
iter, iter,
path::Path, path::Path,
process::{Command, Stdio}, process::{Command, Output, Stdio},
str, str,
}; };
pub(crate) use executable_path::executable_path; pub(crate) use executable_path::executable_path;
pub(crate) use just::unindent; pub(crate) use just::unindent;
pub(crate) use libc::{EXIT_FAILURE, EXIT_SUCCESS}; pub(crate) use libc::{EXIT_FAILURE, EXIT_SUCCESS};
pub(crate) use test_utilities::{assert_stdout, tempdir, tmptree}; pub(crate) use temptree::temptree;
pub(crate) use which::which; pub(crate) use which::which;
pub(crate) use yaml_rust::YamlLoader; pub(crate) use yaml_rust::YamlLoader;
pub(crate) use crate::{
assert_stdout::assert_stdout, assert_success::assert_success, tempdir::tempdir,
};

View File

@ -1,11 +1,8 @@
use std::process::Command; use crate::common::*;
use executable_path::executable_path;
use tempfile::tempdir;
#[test] #[test]
fn output() { fn output() {
let tempdir = tempdir().unwrap(); let tempdir = tempdir();
let output = Command::new(executable_path("just")) let output = Command::new(executable_path("just"))
.arg("--completions") .arg("--completions")

View File

@ -1,11 +1,8 @@
use executable_path::executable_path; use crate::common::*;
use std::{process, str};
use test_utilities::tmptree;
#[test] #[test]
fn dotenv() { fn dotenv() {
let tmp = tmptree! { let tmp = temptree! {
".env": "KEY=ROOT", ".env": "KEY=ROOT",
sub: { sub: {
".env": "KEY=SUB", ".env": "KEY=SUB",
@ -15,7 +12,7 @@ fn dotenv() {
let binary = executable_path("just"); let binary = executable_path("just");
let output = process::Command::new(binary) let output = Command::new(binary)
.current_dir(tmp.path()) .current_dir(tmp.path())
.arg("sub/default") .arg("sub/default")
.output() .output()

View File

@ -5,7 +5,7 @@ const JUSTFILE: &str = "Yooooooo, hopefully this never becomes valid syntax.";
/// Test that --edit doesn't require a valid justfile /// Test that --edit doesn't require a valid justfile
#[test] #[test]
fn invalid_justfile() { fn invalid_justfile() {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
}; };
@ -29,7 +29,7 @@ fn invalid_justfile() {
/// Test that editor is $VISUAL, $EDITOR, or "vim" in that order /// Test that editor is $VISUAL, $EDITOR, or "vim" in that order
#[test] #[test]
fn editor_precedence() { fn editor_precedence() {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
}; };
@ -83,7 +83,7 @@ fn editor_precedence() {
#[cfg(unix)] #[cfg(unix)]
#[test] #[test]
fn editor_working_directory() { fn editor_working_directory() {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
child: {}, child: {},
editor: "#!/usr/bin/env sh\ncat $1\npwd", editor: "#!/usr/bin/env sh\ncat $1\npwd",

View File

@ -1,7 +1,4 @@
use std::{fs, process::Command}; use crate::common::*;
use executable_path::executable_path;
use test_utilities::assert_success;
#[test] #[test]
fn examples() { fn examples() {

View File

@ -1,8 +1,4 @@
use std::{fs, process::Command}; use crate::common::*;
use executable_path::executable_path;
use test_utilities::{tempdir, tmptree};
const EXPECTED: &str = "default:\n\techo 'Hello, world!'\n"; const EXPECTED: &str = "default:\n\techo 'Hello, world!'\n";
@ -47,7 +43,7 @@ fn exists() {
#[test] #[test]
fn invocation_directory() { fn invocation_directory() {
let tmp = tmptree! { let tmp = temptree! {
".git": {}, ".git": {},
}; };
@ -67,7 +63,7 @@ fn invocation_directory() {
#[test] #[test]
fn parent_dir() { fn parent_dir() {
let tmp = tmptree! { let tmp = temptree! {
".git": {}, ".git": {},
sub: {}, sub: {},
}; };
@ -88,7 +84,7 @@ fn parent_dir() {
#[test] #[test]
fn alternate_marker() { fn alternate_marker() {
let tmp = tmptree! { let tmp = temptree! {
"_darcs": {}, "_darcs": {},
}; };
@ -108,7 +104,7 @@ fn alternate_marker() {
#[test] #[test]
fn search_directory() { fn search_directory() {
let tmp = tmptree! { let tmp = temptree! {
sub: { sub: {
".git": {}, ".git": {},
}, },
@ -131,7 +127,7 @@ fn search_directory() {
#[test] #[test]
fn justfile() { fn justfile() {
let tmp = tmptree! { let tmp = temptree! {
sub: { sub: {
".git": {}, ".git": {},
}, },
@ -155,7 +151,7 @@ fn justfile() {
#[test] #[test]
fn justfile_and_working_directory() { fn justfile_and_working_directory() {
let tmp = tmptree! { let tmp = temptree! {
sub: { sub: {
".git": {}, ".git": {},
}, },

View File

@ -1,13 +1,8 @@
#[cfg(unix)] #[cfg(unix)]
mod unix { mod unix {
use executable_path::executable_path; use crate::common::*;
use just::unindent;
use std::{ use std::time::{Duration, Instant};
fs,
process::Command,
time::{Duration, Instant},
};
use test_utilities::tempdir;
fn kill(process_id: u32) { fn kill(process_id: u32) {
unsafe { unsafe {

View File

@ -1,7 +1,4 @@
use std::{fs, path::Path, process, str}; use crate::common::*;
use executable_path::executable_path;
use test_utilities::tempdir;
#[cfg(unix)] #[cfg(unix)]
fn convert_native_path(path: &Path) -> String { fn convert_native_path(path: &Path) -> String {
@ -15,7 +12,7 @@ fn convert_native_path(path: &Path) -> String {
#[cfg(windows)] #[cfg(windows)]
fn convert_native_path(path: &Path) -> String { fn convert_native_path(path: &Path) -> String {
// Translate path from windows style to unix style // Translate path from windows style to unix style
let mut cygpath = process::Command::new("cygpath"); let mut cygpath = Command::new("cygpath");
cygpath.arg("--unix"); cygpath.arg("--unix");
cygpath.arg(path); cygpath.arg(path);
@ -51,7 +48,7 @@ fn test_invocation_directory() {
subdir.push("subdir"); subdir.push("subdir");
fs::create_dir(&subdir).unwrap(); fs::create_dir(&subdir).unwrap();
let output = process::Command::new(&executable_path("just")) let output = Command::new(&executable_path("just"))
.current_dir(&subdir) .current_dir(&subdir)
.args(&["--shell", "sh"]) .args(&["--shell", "sh"])
.output() .output()

View File

@ -1,7 +1,10 @@
#[macro_use] #[macro_use]
mod test; mod test;
mod assert_stdout;
mod assert_success;
mod common; mod common;
mod tempdir;
mod choose; mod choose;
mod command; mod command;

View File

@ -1,7 +1,4 @@
use std::{fs, process::Command}; use crate::common::*;
use executable_path::executable_path;
use test_utilities::{assert_success, tempdir};
#[test] #[test]
fn readme() { fn readme() {

View File

@ -1,12 +1,9 @@
use executable_path::executable_path; use crate::common::*;
use std::{path, process, str};
use test_utilities::tmptree; fn search_test<P: AsRef<Path>>(path: P, args: &[&str]) {
fn search_test<P: AsRef<path::Path>>(path: P, args: &[&str]) {
let binary = executable_path("just"); let binary = executable_path("just");
let output = process::Command::new(binary) let output = Command::new(binary)
.current_dir(path) .current_dir(path)
.args(args) .args(args)
.output() .output()
@ -23,7 +20,7 @@ fn search_test<P: AsRef<path::Path>>(path: P, args: &[&str]) {
#[test] #[test]
fn test_justfile_search() { fn test_justfile_search() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo ok", justfile: "default:\n\techo ok",
a: { a: {
b: { b: {
@ -39,7 +36,7 @@ fn test_justfile_search() {
#[test] #[test]
fn test_capitalized_justfile_search() { fn test_capitalized_justfile_search() {
let tmp = tmptree! { let tmp = temptree! {
Justfile: "default:\n\techo ok", Justfile: "default:\n\techo ok",
a: { a: {
b: { b: {
@ -55,7 +52,7 @@ fn test_capitalized_justfile_search() {
#[test] #[test]
fn test_upwards_path_argument() { fn test_upwards_path_argument() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo ok", justfile: "default:\n\techo ok",
a: { a: {
justfile: "default:\n\techo bad", justfile: "default:\n\techo bad",
@ -68,7 +65,7 @@ fn test_upwards_path_argument() {
#[test] #[test]
fn test_downwards_path_argument() { fn test_downwards_path_argument() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo bad", justfile: "default:\n\techo bad",
a: { a: {
justfile: "default:\n\techo ok", justfile: "default:\n\techo ok",
@ -87,7 +84,7 @@ fn test_downwards_path_argument() {
#[test] #[test]
fn test_upwards_multiple_path_argument() { fn test_upwards_multiple_path_argument() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo ok", justfile: "default:\n\techo ok",
a: { a: {
b: { b: {
@ -103,7 +100,7 @@ fn test_upwards_multiple_path_argument() {
#[test] #[test]
fn test_downwards_multiple_path_argument() { fn test_downwards_multiple_path_argument() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo bad", justfile: "default:\n\techo bad",
a: { a: {
b: { b: {
@ -124,7 +121,7 @@ fn test_downwards_multiple_path_argument() {
#[test] #[test]
fn single_downards() { fn single_downards() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo ok", justfile: "default:\n\techo ok",
child: {}, child: {},
}; };
@ -136,7 +133,7 @@ fn single_downards() {
#[test] #[test]
fn single_upwards() { fn single_upwards() {
let tmp = tmptree! { let tmp = temptree! {
justfile: "default:\n\techo ok", justfile: "default:\n\techo ok",
child: {}, child: {},
}; };

View File

@ -1,8 +1,4 @@
use std::{process::Command, str}; use crate::common::*;
use executable_path::executable_path;
use test_utilities::{assert_stdout, tmptree};
const JUSTFILE: &str = " const JUSTFILE: &str = "
expression := `EXPRESSION` expression := `EXPRESSION`
@ -17,7 +13,7 @@ recipe default=`DEFAULT`:
#[test] #[test]
#[cfg_attr(windows, ignore)] #[cfg_attr(windows, ignore)]
fn flag() { fn flag() {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
shell: "#!/usr/bin/env bash\necho \"$@\"", shell: "#!/usr/bin/env bash\necho \"$@\"",
}; };
@ -56,7 +52,7 @@ recipe:
#[test] #[test]
#[cfg_attr(unix, ignore)] #[cfg_attr(unix, ignore)]
fn cmd() { fn cmd() {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE_CMD, justfile: JUSTFILE_CMD,
}; };
@ -85,7 +81,7 @@ recipe:
#[test] #[test]
#[cfg_attr(unix, ignore)] #[cfg_attr(unix, ignore)]
fn powershell() { fn powershell() {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE_POWERSHELL, justfile: JUSTFILE_POWERSHELL,
}; };

6
tests/tempdir.rs Normal file
View File

@ -0,0 +1,6 @@
pub(crate) fn tempdir() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("just-test-tempdir")
.tempdir()
.expect("failed to create temporary directory")
}

View File

@ -1,7 +1,5 @@
use crate::common::*; use crate::common::*;
pub(crate) use pretty_assertions::assert_eq;
macro_rules! test { macro_rules! test {
( (
name: $name:ident, name: $name:ident,

View File

@ -1,7 +1,4 @@
use std::{error::Error, process::Command}; use crate::common::*;
use executable_path::executable_path;
use test_utilities::tmptree;
const JUSTFILE: &str = r#" const JUSTFILE: &str = r#"
foo := `cat data` foo := `cat data`
@ -24,7 +21,7 @@ const WANT: &str = "shebang: OK\nexpression: OK\ndefault: OK\nlinewise: OK\n";
/// `--justfile` but not `--working-directory` /// `--justfile` but not `--working-directory`
#[test] #[test]
fn justfile_without_working_directory() -> Result<(), Box<dyn Error>> { fn justfile_without_working_directory() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
data: DATA, data: DATA,
}; };
@ -50,7 +47,7 @@ fn justfile_without_working_directory() -> Result<(), Box<dyn Error>> {
/// `--justfile` but not `--working-directory`, and justfile path has no parent /// `--justfile` but not `--working-directory`, and justfile path has no parent
#[test] #[test]
fn justfile_without_working_directory_relative() -> Result<(), Box<dyn Error>> { fn justfile_without_working_directory_relative() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
data: DATA, data: DATA,
}; };
@ -77,7 +74,7 @@ fn justfile_without_working_directory_relative() -> Result<(), Box<dyn Error>> {
/// found /// found
#[test] #[test]
fn change_working_directory_to_search_justfile_parent() -> Result<(), Box<dyn Error>> { fn change_working_directory_to_search_justfile_parent() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
data: DATA, data: DATA,
subdir: {}, subdir: {},
@ -103,7 +100,7 @@ fn change_working_directory_to_search_justfile_parent() -> Result<(), Box<dyn Er
/// `--justfile` but not `--working-directory` /// `--justfile` but not `--working-directory`
#[test] #[test]
fn justfile_and_working_directory() -> Result<(), Box<dyn Error>> { fn justfile_and_working_directory() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! { let tmp = temptree! {
justfile: JUSTFILE, justfile: JUSTFILE,
sub: { sub: {
data: DATA, data: DATA,
@ -133,7 +130,7 @@ fn justfile_and_working_directory() -> Result<(), Box<dyn Error>> {
/// `--justfile` but not `--working-directory` /// `--justfile` but not `--working-directory`
#[test] #[test]
fn search_dir_child() -> Result<(), Box<dyn Error>> { fn search_dir_child() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! { let tmp = temptree! {
child: { child: {
justfile: JUSTFILE, justfile: JUSTFILE,
data: DATA, data: DATA,
@ -161,7 +158,7 @@ fn search_dir_child() -> Result<(), Box<dyn Error>> {
/// `--justfile` but not `--working-directory` /// `--justfile` but not `--working-directory`
#[test] #[test]
fn search_dir_parent() -> Result<(), Box<dyn Error>> { fn search_dir_parent() -> Result<(), Box<dyn Error>> {
let tmp = tmptree! { let tmp = temptree! {
child: { child: {
}, },
justfile: JUSTFILE, justfile: JUSTFILE,