History

move `Env` test utility into `git-testtools` (#331)

Sebastian Thiel committed 4 years ago

Commit bd3f4d01 parent 15a379a5 files · +111 −36Browse files

Unified · Split

5 files
  1. Cargo.lock +78 −2
  2. git-config/src/file/includes.rs +1 −4
  3. git-config/tests/file/init/from_env.rs +2 −27
  4. git-config/tests/file/init/from_paths/includes/conditional/gitdir/mod.rs +4 −3
  5. tests/tools/src/lib.rs +26 −0
Cargo.lock+78−2
@@ -943,6 +943,21 @@ version = "1.2.0" · 942 unmodified lines
943source = "registry+https://github.com/rust-lang/crates.io-index"943source = "registry+https://github.com/rust-lang/crates.io-index"
944checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"944checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
945 945
946[[package]]
947name = "futures"
948version = "0.3.21"
949source = "registry+https://github.com/rust-lang/crates.io-index"
950checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
951dependencies = [
952 "futures-channel",
953 "futures-core",
954 "futures-executor",
955 "futures-io",
956 "futures-sink",
957 "futures-task",
958 "futures-util",
959]
960
946[[package]]961[[package]]
947name = "futures-channel"962name = "futures-channel"
948version = "0.3.21"963version = "0.3.21"
@@ -959,6 +974,17 @@ version = "0.3.21" · 10 unmodified lines
959source = "registry+https://github.com/rust-lang/crates.io-index"974source = "registry+https://github.com/rust-lang/crates.io-index"
960checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"975checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
961 976
977[[package]]
978name = "futures-executor"
979version = "0.3.21"
980source = "registry+https://github.com/rust-lang/crates.io-index"
981checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6"
982dependencies = [
983 "futures-core",
984 "futures-task",
985 "futures-util",
986]
987
962[[package]]988[[package]]
963name = "futures-io"989name = "futures-io"
964version = "0.3.21"990version = "0.3.21"
@@ -986,6 +1012,29 @@ version = "0.3.21" · 21 unmodified lines
986source = "registry+https://github.com/rust-lang/crates.io-index"1012source = "registry+https://github.com/rust-lang/crates.io-index"
987checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868"1013checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868"
988 1014
1015[[package]]
1016name = "futures-task"
1017version = "0.3.21"
1018source = "registry+https://github.com/rust-lang/crates.io-index"
1019checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a"
1020
1021[[package]]
1022name = "futures-util"
1023version = "0.3.21"
1024source = "registry+https://github.com/rust-lang/crates.io-index"
1025checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a"
1026dependencies = [
1027 "futures-channel",
1028 "futures-core",
1029 "futures-io",
1030 "futures-sink",
1031 "futures-task",
1032 "memchr",
1033 "pin-project-lite",
1034 "pin-utils",
1035 "slab",
1036]
1037
989[[package]]1038[[package]]
990name = "generic-array"1039name = "generic-array"
991version = "0.14.5"1040version = "0.14.5"
@@ -1100,7 +1149,7 @@ dependencies = [ · 108 unmodified lines
1100 "nom",1149 "nom",
1101 "serde",1150 "serde",
1102 "serde_derive",1151 "serde_derive",
1103 "serial_test",1152 "serial_test 0.7.0",
1104 "smallvec",1153 "smallvec",
1105 "tempfile",1154 "tempfile",
1106 "thiserror",1155 "thiserror",
@@ -1467,6 +1516,7 @@ dependencies = [ · 360 unmodified lines
1467 "git-worktree",1516 "git-worktree",
1468 "is_ci",1517 "is_ci",
1469 "log",1518 "log",
1519 "serial_test 0.8.0",
1470 "signal-hook",1520 "signal-hook",
1471 "tempfile",1521 "tempfile",
1472 "thiserror",1522 "thiserror",
@@ -2587,7 +2637,20 @@ dependencies = [ · 1114 unmodified lines
2587 "lazy_static",2637 "lazy_static",
2588 "log",2638 "log",
2589 "parking_lot 0.12.1",2639 "parking_lot 0.12.1",
2590 "serial_test_derive",2640 "serial_test_derive 0.7.0",
2641]
2642
2643[[package]]
2644name = "serial_test"
2645version = "0.8.0"
2646source = "registry+https://github.com/rust-lang/crates.io-index"
2647checksum = "7eec42e7232e5ca56aa59d63af3c7f991fe71ee6a3ddd2d3480834cf3902b007"
2648dependencies = [
2649 "futures",
2650 "lazy_static",
2651 "log",
2652 "parking_lot 0.12.1",
2653 "serial_test_derive 0.8.0",
2591]2654]
2592 2655
2593[[package]]2656[[package]]
@@ -2603,6 +2666,19 @@ dependencies = [ · 9 unmodified lines
2603 "syn",2666 "syn",
2604]2667]
2605 2668
2669[[package]]
2670name = "serial_test_derive"
2671version = "0.8.0"
2672source = "registry+https://github.com/rust-lang/crates.io-index"
2673checksum = "f1b95bb2f4f624565e8fe8140c789af7e2082c0e0561b5a82a1b678baa9703dc"
2674dependencies = [
2675 "proc-macro-error",
2676 "proc-macro2",
2677 "quote",
2678 "rustversion",
2679 "syn",
2680]
2681
2606[[package]]2682[[package]]
2607name = "sha-1"2683name = "sha-1"
2608version = "0.10.0"2684version = "0.10.0"
git-config/src/file/includes.rs+1−4
@@ -325,7 +325,7 @@ mod types { · 324 unmodified lines
325 pub conditional: conditional::Context<'a>,325 pub conditional: conditional::Context<'a>,
326 }326 }
327 327
328 impl Options<'_> {328 impl<'a> Options<'a> {
329 /// Provide options to never follow include directives at all.329 /// Provide options to never follow include directives at all.
330 pub fn no_includes() -> Self {330 pub fn no_includes() -> Self {
331 Options {331 Options {
@@ -335,9 +335,6 @@ mod types { · 3 unmodified lines
335 conditional: Default::default(),335 conditional: Default::default(),
336 }336 }
337 }337 }
338 }
339
340 impl<'a> Options<'a> {
341 /// Provide options to follow includes like git does, provided the required `conditional` and `interpolate` contexts338 /// Provide options to follow includes like git does, provided the required `conditional` and `interpolate` contexts
342 /// to support `gitdir` and `onbranch` based `includeIf` directives as well as standard `include.path` resolution.339 /// to support `gitdir` and `onbranch` based `includeIf` directives as well as standard `include.path` resolution.
343 /// Note that the follow-mode is `git`-style, following at most 10 indirections while340 /// Note that the follow-mode is `git`-style, following at most 10 indirections while
git-config/tests/file/init/from_env.rs+2−27
@@ -1,39 +1,14 @@
1use std::{borrow::Cow, env, fs};1use std::{borrow::Cow, fs};
2 2
3use git_config::file::includes;3use git_config::file::includes;
4use git_config::file::init;4use git_config::file::init;
5use git_config::{file::init::from_env, File};5use git_config::{file::init::from_env, File};
6use git_testtools::Env;
6use serial_test::serial;7use serial_test::serial;
7use tempfile::tempdir;8use tempfile::tempdir;
8 9
9use crate::file::init::from_paths::escape_backslashes;10use crate::file::init::from_paths::escape_backslashes;
10 11
11pub struct Env<'a> {
12 altered_vars: Vec<&'a str>,
13}
14
15impl<'a> Env<'a> {
16 pub(crate) fn new() -> Self {
17 Env {
18 altered_vars: Vec::new(),
19 }
20 }
21
22 pub(crate) fn set(mut self, var: &'a str, value: impl Into<String>) -> Self {
23 env::set_var(var, value.into());
24 self.altered_vars.push(var);
25 self
26 }
27}
28
29impl<'a> Drop for Env<'a> {
30 fn drop(&mut self) {
31 for var in &self.altered_vars {
32 env::remove_var(var);
33 }
34 }
35}
36
37#[test]12#[test]
38#[serial]13#[serial]
39fn empty_without_relevant_environment() {14fn empty_without_relevant_environment() {
git-config/tests/file/init/from_paths/includes/conditional/gitdir/mod.rs+4−3
@@ -1,5 +1,6 @@
1mod util;1mod util;
2 2
3use git_testtools::Env;
3use serial_test::serial;4use serial_test::serial;
4use util::{assert_section_value, Condition, GitEnv};5use util::{assert_section_value, Condition, GitEnv};
5 6
@@ -90,7 +91,7 @@ fn dot_slash_from_environment_causes_error() -> crate::Result { · 84 unmodified lines
90 let env = GitEnv::repo_name("worktree")?;91 let env = GitEnv::repo_name("worktree")?;
91 92
92 {93 {
93 let _environment = crate::file::init::from_env::Env::new()94 let _environment = Env::new()
94 .set("GIT_CONFIG_COUNT", "1")95 .set("GIT_CONFIG_COUNT", "1")
95 .set(96 .set(
96 "GIT_CONFIG_KEY_0",97 "GIT_CONFIG_KEY_0",
@@ -112,7 +113,7 @@ fn dot_slash_from_environment_causes_error() -> crate::Result { · 15 unmodified lines
112 113
113 let absolute_path = escape_backslashes(env.home_dir().join("include.config"));114 let absolute_path = escape_backslashes(env.home_dir().join("include.config"));
114 {115 {
115 let _environment = crate::file::init::from_env::Env::new()116 let _environment = Env::new()
116 .set("GIT_CONFIG_COUNT", "1")117 .set("GIT_CONFIG_COUNT", "1")
117 .set("GIT_CONFIG_KEY_0", "includeIf.gitdir:./worktree/.path")118 .set("GIT_CONFIG_KEY_0", "includeIf.gitdir:./worktree/.path")
118 .set("GIT_CONFIG_VALUE_0", &absolute_path);119 .set("GIT_CONFIG_VALUE_0", &absolute_path);
@@ -130,7 +131,7 @@ fn dot_slash_from_environment_causes_error() -> crate::Result { · 11 unmodified lines
130 }131 }
131 132
132 {133 {
133 let _environment = crate::file::init::from_env::Env::new()134 let _environment = Env::new()
134 .set("GIT_CONFIG_COUNT", "1")135 .set("GIT_CONFIG_COUNT", "1")
135 .set(136 .set(
136 "GIT_CONFIG_KEY_0",137 "GIT_CONFIG_KEY_0",
tests/tools/src/lib.rs+26−0
@@ -409,3 +409,29 @@ pub fn sleep_forever() -> ! { · 408 unmodified lines
409 std::thread::sleep(std::time::Duration::from_secs(u64::MAX))409 std::thread::sleep(std::time::Duration::from_secs(u64::MAX))
410 }410 }
411}411}
412
413pub struct Env<'a> {
414 altered_vars: Vec<&'a str>,
415}
416
417impl<'a> Env<'a> {
418 pub fn new() -> Self {
419 Env {
420 altered_vars: Vec::new(),
421 }
422 }
423
424 pub fn set(mut self, var: &'a str, value: impl Into<String>) -> Self {
425 std::env::set_var(var, value.into());
426 self.altered_vars.push(var);
427 self
428 }
429}
430
431impl<'a> Drop for Env<'a> {
432 fn drop(&mut self) {
433 for var in &self.altered_vars {
434 std::env::remove_var(var);
435 }
436 }
437}