]>
Commit | Line | Data |
---|---|---|
e7eda2f9 EH |
1 | //! Tests for `include` config field. |
2 | ||
3 | use super::config::{ | |
4 | assert_error, assert_match, read_output, write_config, write_config_at, ConfigBuilder, | |
5 | }; | |
80e55c77 | 6 | use cargo_test_support::{no_such_file_err_msg, paths}; |
1d5d7a26 | 7 | use std::fs; |
e7eda2f9 EH |
8 | |
9 | #[cargo_test] | |
10 | fn gated() { | |
11 | // Requires -Z flag. | |
12 | write_config("include='other'"); | |
13 | let config = ConfigBuilder::new().build(); | |
14 | let output = read_output(config); | |
15 | let expected = "\ | |
16 | warning: config `include` in `[..]/.cargo/config` ignored, \ | |
17 | the -Zconfig-include command-line flag is required | |
18 | "; | |
19 | assert_match(expected, &output); | |
20 | } | |
21 | ||
22 | #[cargo_test] | |
23 | fn simple() { | |
24 | // Simple test. | |
25 | write_config_at( | |
26 | ".cargo/config", | |
27 | " | |
28 | include = 'other' | |
29 | key1 = 1 | |
30 | key2 = 2 | |
31 | ", | |
32 | ); | |
33 | write_config_at( | |
34 | ".cargo/other", | |
35 | " | |
36 | key2 = 3 | |
37 | key3 = 4 | |
38 | ", | |
39 | ); | |
40 | let config = ConfigBuilder::new().unstable_flag("config-include").build(); | |
41 | assert_eq!(config.get::<i32>("key1").unwrap(), 1); | |
42 | assert_eq!(config.get::<i32>("key2").unwrap(), 2); | |
43 | assert_eq!(config.get::<i32>("key3").unwrap(), 4); | |
44 | } | |
45 | ||
46 | #[cargo_test] | |
47 | fn left_to_right() { | |
48 | // How it merges multiple includes. | |
49 | write_config_at( | |
50 | ".cargo/config", | |
51 | " | |
52 | include = ['one', 'two'] | |
53 | primary = 1 | |
54 | ", | |
55 | ); | |
56 | write_config_at( | |
57 | ".cargo/one", | |
58 | " | |
59 | one = 1 | |
60 | primary = 2 | |
61 | ", | |
62 | ); | |
63 | write_config_at( | |
64 | ".cargo/two", | |
65 | " | |
66 | two = 2 | |
67 | primary = 3 | |
68 | ", | |
69 | ); | |
70 | let config = ConfigBuilder::new().unstable_flag("config-include").build(); | |
71 | assert_eq!(config.get::<i32>("primary").unwrap(), 1); | |
72 | assert_eq!(config.get::<i32>("one").unwrap(), 1); | |
73 | assert_eq!(config.get::<i32>("two").unwrap(), 2); | |
74 | } | |
75 | ||
76 | #[cargo_test] | |
77 | fn missing_file() { | |
78 | // Error when there's a missing file. | |
79 | write_config("include='missing'"); | |
80 | let config = ConfigBuilder::new().unstable_flag("config-include").build(); | |
81 | assert_error( | |
82 | config.get::<i32>("whatever").unwrap_err(), | |
00a47302 EH |
83 | &format!( |
84 | "\ | |
e7eda2f9 EH |
85 | could not load Cargo configuration |
86 | ||
87 | Caused by: | |
88 | failed to load config include `missing` from `[..]/.cargo/config` | |
89 | ||
90 | Caused by: | |
91 | failed to read configuration file `[..]/.cargo/missing` | |
92 | ||
93 | Caused by: | |
00a47302 | 94 | {}", |
80e55c77 | 95 | no_such_file_err_msg() |
00a47302 | 96 | ), |
e7eda2f9 EH |
97 | ); |
98 | } | |
99 | ||
100 | #[cargo_test] | |
101 | fn cycle() { | |
102 | // Detects a cycle. | |
103 | write_config_at(".cargo/config", "include='one'"); | |
104 | write_config_at(".cargo/one", "include='two'"); | |
105 | write_config_at(".cargo/two", "include='config'"); | |
106 | let config = ConfigBuilder::new().unstable_flag("config-include").build(); | |
107 | assert_error( | |
108 | config.get::<i32>("whatever").unwrap_err(), | |
109 | "\ | |
110 | could not load Cargo configuration | |
111 | ||
112 | Caused by: | |
113 | failed to load config include `one` from `[..]/.cargo/config` | |
114 | ||
115 | Caused by: | |
116 | failed to load config include `two` from `[..]/.cargo/one` | |
117 | ||
118 | Caused by: | |
119 | failed to load config include `config` from `[..]/.cargo/two` | |
120 | ||
121 | Caused by: | |
122 | config `include` cycle detected with path `[..]/.cargo/config`", | |
123 | ); | |
124 | } | |
125 | ||
126 | #[cargo_test] | |
127 | fn cli_include() { | |
128 | // Using --config with include. | |
129 | // CLI takes priority over files. | |
130 | write_config_at( | |
131 | ".cargo/config", | |
132 | " | |
133 | foo = 1 | |
134 | bar = 2 | |
135 | ", | |
136 | ); | |
137 | write_config_at(".cargo/config-foo", "foo = 2"); | |
138 | let config = ConfigBuilder::new() | |
139 | .unstable_flag("config-include") | |
140 | .config_arg("include='.cargo/config-foo'") | |
141 | .build(); | |
142 | assert_eq!(config.get::<i32>("foo").unwrap(), 2); | |
143 | assert_eq!(config.get::<i32>("bar").unwrap(), 2); | |
144 | } | |
145 | ||
146 | #[cargo_test] | |
147 | fn bad_format() { | |
148 | // Not a valid format. | |
149 | write_config("include = 1"); | |
150 | let config = ConfigBuilder::new().unstable_flag("config-include").build(); | |
151 | assert_error( | |
152 | config.get::<i32>("whatever").unwrap_err(), | |
153 | "\ | |
154 | could not load Cargo configuration | |
155 | ||
156 | Caused by: | |
157 | `include` expected a string or list, but found integer in `[..]/.cargo/config`", | |
158 | ); | |
159 | } | |
160 | ||
161 | #[cargo_test] | |
162 | fn cli_include_failed() { | |
163 | // Error message when CLI include fails to load. | |
164 | let config = ConfigBuilder::new() | |
165 | .unstable_flag("config-include") | |
166 | .config_arg("include='foobar'") | |
167 | .build_err(); | |
168 | assert_error( | |
169 | config.unwrap_err(), | |
00a47302 EH |
170 | &format!( |
171 | "\ | |
e7eda2f9 | 172 | failed to load --config include |
3a18c89a AC |
173 | |
174 | Caused by: | |
175 | failed to load config include `foobar` from `--config cli option` | |
176 | ||
177 | Caused by: | |
178 | failed to read configuration file `[..]/foobar` | |
179 | ||
180 | Caused by: | |
181 | {}", | |
80e55c77 | 182 | no_such_file_err_msg() |
00a47302 | 183 | ), |
e7eda2f9 EH |
184 | ); |
185 | } | |
186 | ||
187 | #[cargo_test] | |
188 | fn cli_merge_failed() { | |
189 | // Error message when CLI include merge fails. | |
190 | write_config("foo = ['a']"); | |
191 | write_config_at( | |
192 | ".cargo/other", | |
193 | " | |
194 | foo = 'b' | |
195 | ", | |
196 | ); | |
197 | let config = ConfigBuilder::new() | |
198 | .unstable_flag("config-include") | |
199 | .config_arg("include='.cargo/other'") | |
200 | .build_err(); | |
201 | // Maybe this error message should mention it was from an include file? | |
202 | assert_error( | |
203 | config.unwrap_err(), | |
204 | "\ | |
205 | failed to merge --config key `foo` into `[..]/.cargo/config` | |
3a18c89a AC |
206 | |
207 | Caused by: | |
208 | failed to merge config value from `[..]/.cargo/other` into `[..]/.cargo/config`: \ | |
209 | expected array, but found string", | |
e7eda2f9 EH |
210 | ); |
211 | } | |
1d5d7a26 EH |
212 | |
213 | #[cargo_test] | |
214 | fn cli_path() { | |
215 | // --config path_to_file | |
216 | fs::write(paths::root().join("myconfig.toml"), "key = 123").unwrap(); | |
217 | let config = ConfigBuilder::new() | |
218 | .cwd(paths::root()) | |
219 | .unstable_flag("config-include") | |
220 | .config_arg("myconfig.toml") | |
221 | .build(); | |
222 | assert_eq!(config.get::<u32>("key").unwrap(), 123); | |
223 | ||
224 | let config = ConfigBuilder::new() | |
225 | .unstable_flag("config-include") | |
226 | .config_arg("missing.toml") | |
227 | .build_err(); | |
228 | assert_error( | |
229 | config.unwrap_err(), | |
230 | "\ | |
231 | failed to parse --config argument `missing.toml` | |
232 | ||
233 | Caused by: | |
234 | expected an equals, found eof at line 1 column 13", | |
235 | ); | |
236 | } |