Skip to content

Commit

Permalink
fake-editor: replace expectpath with dump-path
Browse files Browse the repository at this point in the history
This allows us to assert the expected path in the test itself, rather
than in the fake editor.
  • Loading branch information
mlcui-corp committed Jul 2, 2024
1 parent a3ca701 commit d811d04
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
13 changes: 5 additions & 8 deletions cli/testing/fake-editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ fn main() {
let dest_path = edit_script_path.parent().unwrap().join(dest);
fs::copy(&args.file, dest_path).unwrap();
}
["dump-path", dest] => {
let dest_path = edit_script_path.parent().unwrap().join(dest);
fs::write(&dest_path, args.file.to_str().unwrap())
.unwrap_or_else(|err| panic!("Failed to write file {dest_path:?}: {err}"));
}
["expect"] => {
let actual = String::from_utf8(fs::read(&args.file).unwrap()).unwrap();
if actual != payload {
Expand All @@ -58,14 +63,6 @@ fn main() {
exit(1)
}
}
["expectpath"] => {
let actual = args.file.to_str().unwrap();
if actual != payload {
eprintln!("fake-editor: Unexpected path.\n");
eprintln!("EXPECTED: <{payload}>\nRECEIVED: <{actual}>");
exit(1)
}
}
["write"] => {
fs::write(&args.file, payload).unwrap_or_else(|_| {
panic!("Failed to write file {}", args.file.to_str().unwrap())
Expand Down
25 changes: 12 additions & 13 deletions cli/tests/test_config_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::PathBuf;

use insta::assert_snapshot;
use itertools::Itertools;
use regex::Regex;
Expand Down Expand Up @@ -621,12 +623,12 @@ fn test_config_edit_user() {
let repo_path = test_env.env_root().join("repo");
let edit_script = test_env.set_up_fake_editor();

std::fs::write(
edit_script,
format!("expectpath\n{}", test_env.config_path().to_str().unwrap()),
)
.unwrap();
std::fs::write(edit_script, "dump-path path").unwrap();
test_env.jj_cmd_ok(&repo_path, &["config", "edit", "--user"]);

let edited_path =
PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path")).unwrap());
assert_eq!(&edited_path, test_env.config_path());
}

#[test]
Expand All @@ -636,15 +638,12 @@ fn test_config_edit_repo() {
let repo_path = test_env.env_root().join("repo");
let edit_script = test_env.set_up_fake_editor();

std::fs::write(
edit_script,
format!(
"expectpath\n{}",
repo_path.join(".jj/repo/config.toml").to_str().unwrap()
),
)
.unwrap();
std::fs::write(edit_script, "dump-path path").unwrap();
test_env.jj_cmd_ok(&repo_path, &["config", "edit", "--repo"]);

let edited_path =
PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path")).unwrap());
assert_eq!(edited_path, repo_path.join(".jj/repo/config.toml"));
}

#[test]
Expand Down

0 comments on commit d811d04

Please sign in to comment.