Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-term/tests/test/helpers.rs')
-rw-r--r--helix-term/tests/test/helpers.rs133
1 files changed, 34 insertions, 99 deletions
diff --git a/helix-term/tests/test/helpers.rs b/helix-term/tests/test/helpers.rs
index 567422c3..112b5e35 100644
--- a/helix-term/tests/test/helpers.rs
+++ b/helix-term/tests/test/helpers.rs
@@ -1,4 +1,5 @@
use std::{
+ fs::File,
io::{Read, Write},
mem::replace,
path::PathBuf,
@@ -6,57 +7,13 @@ use std::{
};
use anyhow::bail;
+use crossterm::event::{Event, KeyEvent};
use helix_core::{diagnostic::Severity, test, Selection, Transaction};
use helix_term::{application::Application, args::Args, config::Config, keymap::merge_keys};
use helix_view::{current_ref, doc, editor::LspConfig, input::parse_macro, Editor};
use tempfile::NamedTempFile;
use tokio_stream::wrappers::UnboundedReceiverStream;
-#[cfg(windows)]
-use crossterm::event::{Event, KeyEvent};
-#[cfg(not(windows))]
-use termina::event::{Event, KeyEvent};
-
-/// Specify how to set up the input text with line feeds
-#[derive(Clone, Debug)]
-pub enum LineFeedHandling {
- /// Replaces all LF chars with the system's appropriate line feed character,
- /// and if one doesn't exist already, appends the system's appropriate line
- /// ending to the end of a string.
- Native,
-
- /// Do not modify the input text in any way. What you give is what you test.
- AsIs,
-}
-
-impl LineFeedHandling {
- /// Apply the line feed handling to the input string, yielding a set of
- /// resulting texts with the appropriate line feed substitutions.
- pub fn apply(&self, text: &str) -> String {
- let line_end = match self {
- LineFeedHandling::Native => helix_core::NATIVE_LINE_ENDING,
- LineFeedHandling::AsIs => return text.into(),
- }
- .as_str();
-
- // we can assume that the source files in this code base will always
- // be LF, so indoc strings will always insert LF
- let mut output = text.replace('\n', line_end);
-
- if !output.ends_with(line_end) {
- output.push_str(line_end);
- }
-
- output
- }
-}
-
-impl Default for LineFeedHandling {
- fn default() -> Self {
- Self::Native
- }
-}
-
#[derive(Clone, Debug)]
pub struct TestCase {
pub in_text: String,
@@ -64,8 +21,6 @@ pub struct TestCase {
pub in_keys: String,
pub out_text: String,
pub out_selection: Selection,
-
- pub line_feed_handling: LineFeedHandling,
}
impl<S, R, V> From<(S, R, V)> for TestCase
@@ -75,19 +30,8 @@ where
V: Into<String>,
{
fn from((input, keys, output): (S, R, V)) -> Self {
- TestCase::from((input, keys, output, LineFeedHandling::default()))
- }
-}
-
-impl<S, R, V> From<(S, R, V, LineFeedHandling)> for TestCase
-where
- S: Into<String>,
- R: Into<String>,
- V: Into<String>,
-{
- fn from((input, keys, output, line_feed_handling): (S, R, V, LineFeedHandling)) -> Self {
- let (in_text, in_selection) = test::print(&line_feed_handling.apply(&input.into()));
- let (out_text, out_selection) = test::print(&line_feed_handling.apply(&output.into()));
+ let (in_text, in_selection) = test::print(&input.into());
+ let (out_text, out_selection) = test::print(&output.into());
TestCase {
in_text,
@@ -95,7 +39,6 @@ where
in_keys: keys.into(),
out_text,
out_selection,
- line_feed_handling,
}
}
}
@@ -194,10 +137,9 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
should_exit: bool,
) -> anyhow::Result<()> {
let test_case = test_case.into();
-
let mut app = match app {
Some(app) => app,
- None => Application::new(Args::default(), test_config(), test_syntax_loader(None))?,
+ None => Application::new(Args::default(), test_config(), test_syntax_conf(None))?,
};
let (view, doc) = helix_view::current!(app.editor);
@@ -220,9 +162,9 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
.await
}
-/// Generates language config loader that merge in overrides, like a user language
+/// Generates language configs that merge in overrides, like a user language
/// config. The argument string must be a raw TOML document.
-pub fn test_syntax_loader(overrides: Option<String>) -> helix_core::syntax::Loader {
+pub fn test_syntax_conf(overrides: Option<String>) -> helix_core::syntax::Configuration {
let mut lang = helix_loader::config::default_lang_config();
if let Some(overrides) = overrides {
@@ -230,7 +172,7 @@ pub fn test_syntax_loader(overrides: Option<String>) -> helix_core::syntax::Load
lang = helix_loader::merge_toml_values(lang, override_toml, 3);
}
- helix_core::syntax::Loader::new(lang.try_into().unwrap()).unwrap()
+ lang.try_into().unwrap()
}
/// Use this for very simple test cases where there is one input
@@ -298,6 +240,23 @@ pub fn test_editor_config() -> helix_view::editor::Config {
}
}
+/// Replaces all LF chars with the system's appropriate line feed
+/// character, and if one doesn't exist already, appends the system's
+/// appropriate line ending to the end of a string.
+pub fn platform_line(input: &str) -> String {
+ let line_end = helix_core::NATIVE_LINE_ENDING.as_str();
+
+ // we can assume that the source files in this code base will always
+ // be LF, so indoc strings will always insert LF
+ let mut output = input.replace('\n', line_end);
+
+ if !output.ends_with(line_end) {
+ output.push_str(line_end);
+ }
+
+ output
+}
+
/// Creates a new temporary file that is set to read only. Useful for
/// testing write failures.
pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> {
@@ -309,22 +268,10 @@ pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> {
Ok(file)
}
-/// Creates a new temporary file in the directory that is set to read only. Useful for
-/// testing write failures.
-pub fn new_readonly_tempfile_in_dir(
- dir: impl AsRef<std::path::Path>,
-) -> anyhow::Result<NamedTempFile> {
- let mut file = tempfile::NamedTempFile::new_in(dir)?;
- let metadata = file.as_file().metadata()?;
- let mut perms = metadata.permissions();
- perms.set_readonly(true);
- file.as_file_mut().set_permissions(perms)?;
- Ok(file)
-}
pub struct AppBuilder {
args: Args,
config: Config,
- syn_loader: helix_core::syntax::Loader,
+ syn_conf: helix_core::syntax::Configuration,
input: Option<(String, Selection)>,
}
@@ -333,7 +280,7 @@ impl Default for AppBuilder {
Self {
args: Args::default(),
config: test_config(),
- syn_loader: test_syntax_loader(None),
+ syn_conf: test_syntax_conf(None),
input: None,
}
}
@@ -349,10 +296,7 @@ impl AppBuilder {
path: P,
pos: Option<helix_core::Position>,
) -> Self {
- self.args
- .files
- .insert(path.into(), vec![pos.unwrap_or_default()]);
-
+ self.args.files.push((path.into(), pos.unwrap_or_default()));
self
}
@@ -370,8 +314,8 @@ impl AppBuilder {
self
}
- pub fn with_lang_loader(mut self, syn_loader: helix_core::syntax::Loader) -> Self {
- self.syn_loader = syn_loader;
+ pub fn with_lang_config(mut self, syn_conf: helix_core::syntax::Configuration) -> Self {
+ self.syn_conf = syn_conf;
self
}
@@ -384,7 +328,7 @@ impl AppBuilder {
bail!("Having the directory {path:?} in args.files[0] is not yet supported for integration tests");
}
- let mut app = Application::new(self.args, self.config, self.syn_loader)?;
+ let mut app = Application::new(self.args, self.config, self.syn_conf)?;
if let Some((text, selection)) = self.input {
let (view, doc) = helix_view::current!(app.editor);
@@ -408,8 +352,9 @@ pub async fn run_event_loop_until_idle(app: &mut Application) {
app.event_loop_until_idle(&mut rx_stream).await;
}
-pub fn assert_file_has_content(file: &mut NamedTempFile, content: &str) -> anyhow::Result<()> {
- reload_file(file)?;
+pub fn assert_file_has_content(file: &mut File, content: &str) -> anyhow::Result<()> {
+ file.flush()?;
+ file.sync_all()?;
let mut file_content = String::new();
file.read_to_string(&mut file_content)?;
@@ -423,13 +368,3 @@ pub fn assert_status_not_error(editor: &Editor) {
assert_ne!(&Severity::Error, sev);
}
}
-
-pub fn reload_file(file: &mut NamedTempFile) -> anyhow::Result<()> {
- let path = file.path();
- let f = std::fs::OpenOptions::new()
- .write(true)
- .read(true)
- .open(&path)?;
- *file.as_file_mut() = f;
- Ok(())
-}