#![cfg(feature = "plugins")]
use crate::common::harness::{copy_plugin, copy_plugin_lib, EditorTestHarness};
use crossterm::event::{KeyCode, KeyModifiers};
use std::fs;
use std::path::Path;
fn set_up_workspace(sentinel: &Path) -> (tempfile::TempDir, std::path::PathBuf) {
fresh::i18n::set_locale("en");
let temp = tempfile::tempdir().unwrap();
let workspace = temp.path().canonicalize().unwrap();
let dc = workspace.join(".devcontainer");
fs::create_dir_all(&dc).unwrap();
let dc_json = format!(
r#"{{
"name": "fake",
"image": "ubuntu:22.04",
"initializeCommand": "touch {}"
}}"#,
sentinel.display()
);
fs::write(dc.join("devcontainer.json"), dc_json).unwrap();
let plugins_dir = workspace.join("plugins");
fs::create_dir_all(&plugins_dir).unwrap();
copy_plugin_lib(&plugins_dir);
copy_plugin(&plugins_dir, "devcontainer");
(temp, workspace)
}
fn wait_for_run_lifecycle_command(harness: &mut EditorTestHarness) {
let want_key = "%cmd.run_lifecycle";
let want_localized = "Dev Container: Run Lifecycle Command";
harness
.wait_until(|h| {
let reg = h.editor().command_registry().read().unwrap();
reg.get_all()
.iter()
.any(|c| c.name == want_key && c.get_localized_name() == want_localized)
})
.unwrap();
}
#[cfg(unix)]
#[test]
fn run_lifecycle_executes_initialize_command() {
let sentinel_temp = tempfile::tempdir().unwrap();
let sentinel = sentinel_temp
.path()
.canonicalize()
.unwrap()
.join("lifecycle.marker");
let (_workspace_temp, workspace) = set_up_workspace(&sentinel);
let mut harness = EditorTestHarness::with_working_dir(160, 40, workspace).unwrap();
harness.tick_and_render().unwrap();
let plugin_names: Vec<_> = harness
.editor()
.plugin_manager()
.list_plugins()
.into_iter()
.map(|p| p.name)
.collect();
assert!(
plugin_names.iter().any(|n| n == "devcontainer"),
"`devcontainer` plugin must be loaded. Loaded: {:?}",
plugin_names
);
wait_for_run_lifecycle_command(&mut harness);
harness
.send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
.unwrap();
harness.wait_for_prompt().unwrap();
harness.type_text("Dev Container: Run Lifecycle").unwrap();
harness
.wait_until(|h| {
h.screen_to_string()
.contains("Dev Container: Run Lifecycle Command")
})
.unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness
.wait_until(|h| h.screen_to_string().contains("initializeCommand"))
.unwrap();
harness
.send_key(KeyCode::Enter, KeyModifiers::NONE)
.unwrap();
harness.wait_until(|_| sentinel.exists()).unwrap();
}