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

View File

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

View File

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

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]
fn default() {
let tmp = tmptree! {
let tmp = temptree! {
justfile: "foo:\n echo foo\n",
};

View File

@ -1,17 +1,22 @@
pub(crate) use std::{
collections::BTreeMap,
env::{self, consts::EXE_SUFFIX},
error::Error,
fs,
io::Write,
iter,
path::Path,
process::{Command, Stdio},
process::{Command, Output, Stdio},
str,
};
pub(crate) use executable_path::executable_path;
pub(crate) use just::unindent;
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 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 executable_path::executable_path;
use tempfile::tempdir;
use crate::common::*;
#[test]
fn output() {
let tempdir = tempdir().unwrap();
let tempdir = tempdir();
let output = Command::new(executable_path("just"))
.arg("--completions")

View File

@ -1,11 +1,8 @@
use executable_path::executable_path;
use std::{process, str};
use test_utilities::tmptree;
use crate::common::*;
#[test]
fn dotenv() {
let tmp = tmptree! {
let tmp = temptree! {
".env": "KEY=ROOT",
sub: {
".env": "KEY=SUB",
@ -15,7 +12,7 @@ fn dotenv() {
let binary = executable_path("just");
let output = process::Command::new(binary)
let output = Command::new(binary)
.current_dir(tmp.path())
.arg("sub/default")
.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]
fn invalid_justfile() {
let tmp = tmptree! {
let tmp = temptree! {
justfile: JUSTFILE,
};
@ -29,7 +29,7 @@ fn invalid_justfile() {
/// Test that editor is $VISUAL, $EDITOR, or "vim" in that order
#[test]
fn editor_precedence() {
let tmp = tmptree! {
let tmp = temptree! {
justfile: JUSTFILE,
};
@ -83,7 +83,7 @@ fn editor_precedence() {
#[cfg(unix)]
#[test]
fn editor_working_directory() {
let tmp = tmptree! {
let tmp = temptree! {
justfile: JUSTFILE,
child: {},
editor: "#!/usr/bin/env sh\ncat $1\npwd",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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