2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-03-05 06:33:15 +00:00
|
|
|
use std::cell::RefCell;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2022-01-16 06:06:12 +00:00
|
|
|
use tempfile::TempDir;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-01-16 06:06:12 +00:00
|
|
|
pub struct TestEnvironment {
|
|
|
|
_temp_dir: TempDir,
|
|
|
|
env_root: PathBuf,
|
|
|
|
home_dir: PathBuf,
|
2022-03-19 17:37:13 +00:00
|
|
|
config_path: PathBuf,
|
2022-03-05 06:33:15 +00:00
|
|
|
command_number: RefCell<i64>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 06:06:12 +00:00
|
|
|
impl Default for TestEnvironment {
|
|
|
|
fn default() -> Self {
|
|
|
|
let tmp_dir = TempDir::new().unwrap();
|
|
|
|
let env_root = tmp_dir.path().canonicalize().unwrap();
|
|
|
|
let home_dir = env_root.join("home");
|
2022-03-13 15:14:01 +00:00
|
|
|
std::fs::create_dir(&home_dir).unwrap();
|
2022-03-19 17:37:13 +00:00
|
|
|
let config_path = env_root.join("config.toml");
|
|
|
|
std::fs::write(&config_path, b"").unwrap();
|
2022-01-16 06:06:12 +00:00
|
|
|
Self {
|
|
|
|
_temp_dir: tmp_dir,
|
|
|
|
env_root,
|
|
|
|
home_dir,
|
2022-03-19 17:37:13 +00:00
|
|
|
config_path,
|
2022-03-05 06:33:15 +00:00
|
|
|
command_number: RefCell::new(0),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 06:06:12 +00:00
|
|
|
impl TestEnvironment {
|
|
|
|
pub fn jj_cmd(&self, current_dir: &Path, args: &[&str]) -> assert_cmd::Command {
|
|
|
|
let mut cmd = assert_cmd::Command::cargo_bin("jj").unwrap();
|
|
|
|
cmd.current_dir(current_dir);
|
|
|
|
cmd.args(args);
|
2022-03-06 02:35:19 +00:00
|
|
|
cmd.env_clear();
|
2022-03-12 06:27:39 +00:00
|
|
|
cmd.env("RUST_BACKTRACE", "1");
|
2022-01-16 06:06:12 +00:00
|
|
|
cmd.env("HOME", self.home_dir.to_str().unwrap());
|
2022-03-05 06:33:15 +00:00
|
|
|
let timestamp = chrono::DateTime::parse_from_rfc3339("2001-02-03T04:05:06+07:00").unwrap();
|
|
|
|
let mut command_number = self.command_number.borrow_mut();
|
|
|
|
*command_number += 1;
|
2022-03-19 17:37:13 +00:00
|
|
|
cmd.env("JJ_CONFIG", self.config_path.to_str().unwrap());
|
2022-03-05 06:33:15 +00:00
|
|
|
let timestamp = timestamp + chrono::Duration::seconds(*command_number);
|
|
|
|
cmd.env("JJ_TIMESTAMP", timestamp.to_rfc3339());
|
2022-03-06 02:51:09 +00:00
|
|
|
cmd.env("JJ_USER", "Test User");
|
|
|
|
cmd.env("JJ_EMAIL", "test.user@example.com");
|
2022-01-16 06:06:12 +00:00
|
|
|
cmd
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-03-26 21:06:48 +00:00
|
|
|
/// Run a `jj` command, check that it was successful, and return its stdout
|
|
|
|
pub fn jj_cmd_success(&self, current_dir: &Path, args: &[&str]) -> String {
|
2022-03-28 03:08:54 +00:00
|
|
|
let assert = self.jj_cmd(current_dir, args).assert().success().stderr("");
|
2022-03-26 21:06:48 +00:00
|
|
|
get_stdout_string(&assert)
|
|
|
|
}
|
|
|
|
|
2022-01-16 06:06:12 +00:00
|
|
|
pub fn env_root(&self) -> &Path {
|
|
|
|
&self.env_root
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2021-08-11 18:18:37 +00:00
|
|
|
|
2022-01-16 06:06:12 +00:00
|
|
|
pub fn home_dir(&self) -> &Path {
|
|
|
|
&self.home_dir
|
2021-08-11 18:18:37 +00:00
|
|
|
}
|
2022-03-19 17:37:13 +00:00
|
|
|
|
|
|
|
pub fn config_path(&self) -> &Path {
|
|
|
|
&self.config_path
|
|
|
|
}
|
2021-08-11 18:18:37 +00:00
|
|
|
}
|
2022-03-03 00:23:30 +00:00
|
|
|
|
2022-03-02 23:09:32 +00:00
|
|
|
pub fn get_stdout_string(assert: &assert_cmd::assert::Assert) -> String {
|
|
|
|
String::from_utf8(assert.get_output().stdout.clone()).unwrap()
|
|
|
|
}
|