Skip to content

Commit

Permalink
Wrap set_var, remove_var in unsafe blocks
Browse files Browse the repository at this point in the history
Rust 2024 is making these particular function unsafe, and this change
prepares the project by making a best effort to change environment
variables in a safe way.

Since these values are only modified in tests, and generally only
certain variables, this change should be generally safe. It also has
zero direct impact on the production build.
  • Loading branch information
MitMaro committed Jun 10, 2024
1 parent a26eee4 commit a9e8c40
Show file tree
Hide file tree
Showing 11 changed files with 554 additions and 311 deletions.
34 changes: 17 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ keywords = ["git", "editor", "tool", "rebase", "cli"]
categories = ["command-line-interface", "command-line-utilities", "text-editors"]
readme = "README.md"
include = [
"**/*.rs",
"/Cargo.toml",
"/CHANGELOG.md",
"/LICENSE",
"/readme",
"/README.md"
"**/*.rs",
"/Cargo.toml",
"/CHANGELOG.md",
"/LICENSE",
"/readme",
"/README.md"
]
edition = "2021"

Expand Down Expand Up @@ -78,20 +78,20 @@ Full feature terminal based sequence editor for git interactive rebase."""
section = "utility"
priority = "optional"
assets = [
["target/release/interactive-rebase-tool", "usr/bin/interactive-rebase-tool", "755"],
["README.md", "usr/share/doc/interactive-rebase-tool/", "644"],
["readme/**/*.md", "usr/share/doc/interactive-rebase-tool/readme/", "644"],
["CHANGELOG.md", "usr/share/doc/interactive-rebase-tool/", "644"],
["src/interactive-rebase-tool.1", "usr/share/man/man1/interactive-rebase-tool.1", "644"]
["target/release/interactive-rebase-tool", "usr/bin/interactive-rebase-tool", "755"],
["README.md", "usr/share/doc/interactive-rebase-tool/", "644"],
["readme/**/*.md", "usr/share/doc/interactive-rebase-tool/readme/", "644"],
["CHANGELOG.md", "usr/share/doc/interactive-rebase-tool/", "644"],
["src/interactive-rebase-tool.1", "usr/share/man/man1/interactive-rebase-tool.1", "644"]
]

[package.metadata.generate-rpm]
assets = [
{ source = "target/release/interactive-rebase-tool", dest = "/usr/bin/interactive-rebase-tool", mode = "755" },
{ source = "README.md", dest = "/usr/share/doc/interactive-rebase-tool/", mode = "644" },
{ source = "readme/*.md", dest = "/usr/share/doc/interactive-rebase-tool/readme/", mode = "644" },
{ source = "CHANGELOG.md", dest = "/usr/share/doc/interactive-rebase-tool/", mode = "644" },
{ source = "src/interactive-rebase-tool.1", dest = "/usr/share/man/man1/interactive-rebase-tool.1", mode = "644" },
{ source = "target/release/interactive-rebase-tool", dest = "/usr/bin/interactive-rebase-tool", mode = "755" },
{ source = "README.md", dest = "/usr/share/doc/interactive-rebase-tool/", mode = "644" },
{ source = "readme/*.md", dest = "/usr/share/doc/interactive-rebase-tool/readme/", mode = "644" },
{ source = "CHANGELOG.md", dest = "/usr/share/doc/interactive-rebase-tool/", mode = "644" },
{ source = "src/interactive-rebase-tool.1", dest = "/usr/share/man/man1/interactive-rebase-tool.1", mode = "644" },
]

[lints.rust]
Expand All @@ -103,7 +103,7 @@ nonstandard_style = { level = "warn", priority = -2 }
rust_2018_compatibility = { level = "warn", priority = -2 }
rust_2018_idioms = { level = "warn", priority = -2 }
rust_2021_compatibility = { level = "warn", priority = -2 }
# rust_2024_compatibility = { level = "warn", priority = -2 } - requires some significant changes
rust_2024_compatibility = { level = "warn", priority = -2 }
unused = { level = "warn", priority = -2 }

unknown_lints = { level = "warn", priority = -1 }
Expand Down
238 changes: 119 additions & 119 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {
create_config,
create_event_reader,
mocks,
set_git_directory,
with_git_directory,
DefaultTestModule,
TestModuleProvider,
},
Expand Down Expand Up @@ -245,31 +245,31 @@ mod tests {
}

#[test]
#[serial_test::serial]
fn load_repository_failure() {
_ = set_git_directory("fixtures/not-a-repository");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> =
Application::new(&args(&["todofile"]), event_provider, create_mocked_crossterm());
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::StateError);
assert!(
exit.get_message()
.as_ref()
.unwrap()
.contains("Unable to load Git repository: ")
);
with_git_directory("fixtures/not-a-repository", |_| {
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> =
Application::new(&args(&["todofile"]), event_provider, create_mocked_crossterm());
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::StateError);
assert!(
exit.get_message()
.as_ref()
.unwrap()
.contains("Unable to load Git repository: ")
);
});
}

#[test]
#[serial_test::serial]
fn load_config_failure() {
_ = set_git_directory("fixtures/invalid-config");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> =
Application::new(&args(&["rebase-todo"]), event_provider, create_mocked_crossterm());
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::ConfigError);
with_git_directory("fixtures/invalid-config", |_| {
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> =
Application::new(&args(&["rebase-todo"]), event_provider, create_mocked_crossterm());
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::ConfigError);
});
}

#[test]
Expand Down Expand Up @@ -303,50 +303,50 @@ mod tests {
}

#[test]
#[serial_test::serial]
fn load_todo_file_load_error() {
_ = set_git_directory("fixtures/simple");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> =
Application::new(&args(&["does-not-exist"]), event_provider, create_mocked_crossterm());
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::FileReadError);
with_git_directory("fixtures/simple", |_| {
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> =
Application::new(&args(&["does-not-exist"]), event_provider, create_mocked_crossterm());
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::FileReadError);
});
}

#[test]
#[serial_test::serial]
fn load_todo_file_noop() {
let git_dir = set_git_directory("fixtures/simple");
let rebase_todo = format!("{git_dir}/rebase-todo-noop");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
);
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::Good);
with_git_directory("fixtures/simple", |git_dir| {
let rebase_todo = format!("{git_dir}/rebase-todo-noop");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
);
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::Good);
});
}

#[test]
#[serial_test::serial]
fn load_todo_file_empty() {
let git_dir = set_git_directory("fixtures/simple");
let rebase_todo = format!("{git_dir}/rebase-todo-empty");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
);
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::Good);
assert!(
exit.get_message()
.as_ref()
.unwrap()
.contains("An empty rebase was provided, nothing to edit")
);
with_git_directory("fixtures/simple", |git_dir| {
let rebase_todo = format!("{git_dir}/rebase-todo-empty");
let event_provider = create_event_reader(|| Ok(None));
let application: Result<Application<TestModuleProvider<DefaultTestModule>>, Exit> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
);
let exit = application_error!(application);
assert_eq!(exit.get_status(), &ExitStatus::Good);
assert!(
exit.get_message()
.as_ref()
.unwrap()
.contains("An empty rebase was provided, nothing to edit")
);
});
}

#[test]
Expand All @@ -363,22 +363,21 @@ mod tests {
}

#[test]
#[serial_test::serial]
fn run_until_finished_success() {
let git_dir = set_git_directory("fixtures/simple");
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| Ok(Some(Event::Key(KeyEvent::from(KeyCode::Char('W'))))));
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
assert_ok!(application.run_until_finished());
with_git_directory("fixtures/simple", |git_dir| {
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| Ok(Some(Event::Key(KeyEvent::from(KeyCode::Char('W'))))));
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
assert_ok!(application.run_until_finished());
});
}

#[test]
#[serial_test::serial]
fn run_join_error() {
struct FailingThread;
impl Threadable for FailingThread {
Expand All @@ -391,67 +390,68 @@ mod tests {
}
}

let git_dir = set_git_directory("fixtures/simple");
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| Ok(Some(Event::Key(KeyEvent::from(KeyCode::Char('W'))))));
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
with_git_directory("fixtures/simple", |git_dir| {
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| Ok(Some(Event::Key(KeyEvent::from(KeyCode::Char('W'))))));
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();

application.threads = Some(vec![Box::new(FailingThread {})]);
application.threads = Some(vec![Box::new(FailingThread {})]);

let exit = application.run_until_finished().unwrap_err();
assert_eq!(exit.get_status(), &ExitStatus::StateError);
assert!(
exit.get_message()
.as_ref()
.unwrap()
.starts_with("Failed to join runtime:")
);
let exit = application.run_until_finished().unwrap_err();
assert_eq!(exit.get_status(), &ExitStatus::StateError);
assert!(
exit.get_message()
.as_ref()
.unwrap()
.starts_with("Failed to join runtime:")
);
});
}

#[test]
#[serial_test::serial]
fn run_until_finished_kill() {
let git_dir = set_git_directory("fixtures/simple");
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| {
Ok(Some(Event::Key(KeyEvent::new(
KeyCode::Char('c'),
KeyModifiers::CONTROL,
))))
with_git_directory("fixtures/simple", |git_dir| {
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| {
Ok(Some(Event::Key(KeyEvent::new(
KeyCode::Char('c'),
KeyModifiers::CONTROL,
))))
});
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
let exit = application.run_until_finished().unwrap_err();
assert_eq!(exit.get_status(), &ExitStatus::Kill);
});
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
let exit = application.run_until_finished().unwrap_err();
assert_eq!(exit.get_status(), &ExitStatus::Kill);
}

#[test]
#[serial_test::serial]
fn run_error_on_second_attempt() {
let git_dir = set_git_directory("fixtures/simple");
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| Ok(Some(Event::Key(KeyEvent::from(KeyCode::Char('W'))))));
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
assert_ok!(application.run_until_finished());
let exit = application.run_until_finished().unwrap_err();
assert_eq!(exit.get_status(), &ExitStatus::StateError);
assert_eq!(
exit.get_message().as_ref().unwrap(),
"Attempt made to run application a second time"
);
with_git_directory("fixtures/simple", |git_dir| {
let rebase_todo = format!("{git_dir}/rebase-todo");
let event_provider = create_event_reader(|| Ok(Some(Event::Key(KeyEvent::from(KeyCode::Char('W'))))));
let mut application: Application<Modules> = Application::new(
&args(&[rebase_todo.as_str()]),
event_provider,
create_mocked_crossterm(),
)
.unwrap();
assert_ok!(application.run_until_finished());
let exit = application.run_until_finished().unwrap_err();
assert_eq!(exit.get_status(), &ExitStatus::StateError);
assert_eq!(
exit.get_message().as_ref().unwrap(),
"Attempt made to run application a second time"
);
});
}
}
Loading

0 comments on commit a9e8c40

Please sign in to comment.