Skip to content

Commit fcf022d

Browse files
authored
Merge pull request #326 from moonbitlang/refactor-test-util
internal: refactor test utils
2 parents 193c79f + 258d9f0 commit fcf022d

File tree

3 files changed

+424
-511
lines changed

3 files changed

+424
-511
lines changed

crates/moon/tests/mod.rs

Lines changed: 24 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818

1919
mod cmd_test;
2020
mod test_cases;
21+
mod util;
2122

22-
use expect_test::Expect;
2323
use std::path::{Path, PathBuf};
24-
25-
fn check(actual: &str, expect: Expect) {
26-
expect.assert_eq(actual)
27-
}
24+
use util::*;
2825

2926
struct TestDir {
3027
// tempfile::TempDir has a drop implementation that will remove the directory
@@ -60,12 +57,8 @@ impl AsRef<Path> for TestDir {
6057
}
6158
}
6259

63-
pub fn moon_bin() -> PathBuf {
64-
snapbox::cmd::cargo_bin("moon")
65-
}
66-
6760
#[track_caller]
68-
pub fn get_stdout_with_args_without_replace(
61+
fn get_stdout_without_replace(
6962
dir: &impl AsRef<std::path::Path>,
7063
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
7164
) -> String {
@@ -83,7 +76,7 @@ pub fn get_stdout_with_args_without_replace(
8376
}
8477

8578
#[track_caller]
86-
pub fn get_stderr_on_success_with_args_without_replace(
79+
fn get_stderr_without_replace(
8780
dir: &impl AsRef<std::path::Path>,
8881
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
8982
) -> String {
@@ -100,8 +93,7 @@ pub fn get_stderr_on_success_with_args_without_replace(
10093
s
10194
}
10295

103-
#[track_caller]
104-
pub fn get_stderr_with_args_without_replace(
96+
fn get_err_stdout_without_replace(
10597
dir: &impl AsRef<std::path::Path>,
10698
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
10799
) -> String {
@@ -111,72 +103,15 @@ pub fn get_stderr_with_args_without_replace(
111103
.assert()
112104
.failure()
113105
.get_output()
114-
.stderr
106+
.stdout
115107
.to_owned();
116108

117109
let s = std::str::from_utf8(&out).unwrap().to_string();
118110
s
119111
}
120112

121113
#[track_caller]
122-
pub fn get_stdout_with_args(
123-
dir: &impl AsRef<std::path::Path>,
124-
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
125-
) -> String {
126-
let s = get_stdout_with_args_without_replace(dir, args);
127-
let s = s.replace("\r\n", "\n");
128-
129-
s.replace('\\', "/")
130-
}
131-
132-
pub fn replace_dir(s: &str, dir: &impl AsRef<std::path::Path>) -> String {
133-
let path_str1 = dunce::canonicalize(dir)
134-
.unwrap()
135-
.to_str()
136-
.unwrap()
137-
.to_string();
138-
// for something like "{...\"loc\":{\"path\":\"C:\\\\Users\\\\runneradmin\\\\AppData\\\\Local\\\\Temp\\\\.tmpP0u4VZ\\\\main\\\\main.mbt\"...\r\n" on windows
139-
// https://github.com/moonbitlang/moon/actions/runs/10092428950/job/27906057649#step:13:149
140-
let s = s.replace("\\\\", "\\");
141-
let s = s.replace(&path_str1, "$ROOT");
142-
let s = s.replace(
143-
dunce::canonicalize(moonutil::moon_dir::home())
144-
.unwrap()
145-
.to_str()
146-
.unwrap(),
147-
"$MOON_HOME",
148-
);
149-
let s = s.replace(moon_bin().to_string_lossy().as_ref(), "moon");
150-
s.replace("\r\n", "\n").replace('\\', "/")
151-
}
152-
153-
#[track_caller]
154-
pub fn get_stdout_with_args_and_replace_dir(
155-
dir: &impl AsRef<std::path::Path>,
156-
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
157-
) -> String {
158-
let s = get_stdout_with_args_without_replace(dir, args);
159-
replace_dir(&s, dir)
160-
}
161-
162-
pub fn get_stderr_with_args_and_replace_dir(
163-
dir: &impl AsRef<std::path::Path>,
164-
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
165-
) -> String {
166-
let s = get_stderr_with_args_without_replace(dir, args);
167-
replace_dir(&s, dir)
168-
}
169-
170-
#[track_caller]
171-
pub fn get_stderr_on_success_with_args_and_replace_dir(
172-
dir: &impl AsRef<std::path::Path>,
173-
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
174-
) -> String {
175-
let s = get_stderr_on_success_with_args_without_replace(dir, args);
176-
replace_dir(&s, dir)
177-
}
178-
179-
pub fn get_stderr_with_args(
114+
fn get_err_stderr_without_replace(
180115
dir: &impl AsRef<std::path::Path>,
181116
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
182117
) -> String {
@@ -190,84 +125,39 @@ pub fn get_stderr_with_args(
190125
.to_owned();
191126

192127
let s = std::str::from_utf8(&out).unwrap().to_string();
193-
let s = s.replace("\r\n", "\n");
194-
195-
s.replace('\\', "/")
128+
s
196129
}
197130

198-
pub fn get_stderr_on_success_with_args(
131+
#[track_caller]
132+
pub fn get_stdout(
199133
dir: &impl AsRef<std::path::Path>,
200134
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
201135
) -> String {
202-
let out = snapbox::cmd::Command::new(moon_bin())
203-
.current_dir(dir)
204-
.args(args)
205-
.assert()
206-
.success()
207-
.get_output()
208-
.stderr
209-
.to_owned();
210-
211-
let s = std::str::from_utf8(&out).unwrap().to_string();
212-
let s = s.replace("\r\n", "\n");
213-
214-
s.replace('\\', "/")
215-
}
216-
217-
pub fn copy(src: &Path, dest: &Path) -> anyhow::Result<()> {
218-
if src.is_dir() {
219-
if !dest.exists() {
220-
std::fs::create_dir_all(dest)?;
221-
}
222-
for entry in walkdir::WalkDir::new(src) {
223-
let entry = entry?;
224-
let path = entry.path();
225-
let relative_path = path.strip_prefix(src)?;
226-
let dest_path = dest.join(relative_path);
227-
if path.is_dir() {
228-
if !dest_path.exists() {
229-
std::fs::create_dir_all(dest_path)?;
230-
}
231-
} else {
232-
std::fs::copy(path, dest_path)?;
233-
}
234-
}
235-
} else {
236-
std::fs::copy(src, dest)?;
237-
}
238-
Ok(())
239-
}
240-
241-
pub fn replace_crlf_to_lf(s: &str) -> String {
242-
s.replace("\r\n", "\n")
136+
let s = get_stdout_without_replace(dir, args);
137+
replace_dir(&s, dir)
243138
}
244139

245-
pub fn get_err_stdout_with_args_without_replace(
140+
#[track_caller]
141+
pub fn get_stderr(
246142
dir: &impl AsRef<std::path::Path>,
247143
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
248144
) -> String {
249-
let out = snapbox::cmd::Command::new(moon_bin())
250-
.current_dir(dir)
251-
.args(args)
252-
.assert()
253-
.failure()
254-
.get_output()
255-
.stdout
256-
.to_owned();
257-
258-
let s = std::str::from_utf8(&out).unwrap().to_string();
259-
s
145+
let s = get_stderr_without_replace(dir, args);
146+
replace_dir(&s, dir)
260147
}
261148

262-
pub fn get_err_stdout_with_args_and_replace_dir(
149+
pub fn get_err_stdout(
263150
dir: &impl AsRef<std::path::Path>,
264151
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
265152
) -> String {
266-
let s = get_err_stdout_with_args_without_replace(dir, args);
153+
let s = get_err_stdout_without_replace(dir, args);
267154
replace_dir(&s, dir)
268155
}
269156

270-
#[track_caller]
271-
fn read(p: &Path) -> String {
272-
std::fs::read_to_string(p).unwrap().replace('\r', "")
157+
pub fn get_err_stderr(
158+
dir: &impl AsRef<std::path::Path>,
159+
args: impl IntoIterator<Item = impl AsRef<std::ffi::OsStr>>,
160+
) -> String {
161+
let s = get_err_stderr_without_replace(dir, args);
162+
replace_dir(&s, dir)
273163
}

0 commit comments

Comments
 (0)