@owenlamont/ryl 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/.github/CODEOWNERS +1 -0
  2. package/.github/dependabot.yml +13 -0
  3. package/.github/workflows/ci.yml +107 -0
  4. package/.github/workflows/release.yml +613 -0
  5. package/.github/workflows/update_dependencies.yml +61 -0
  6. package/.github/workflows/update_linters.yml +56 -0
  7. package/.pre-commit-config.yaml +87 -0
  8. package/.yamllint +4 -0
  9. package/AGENTS.md +200 -0
  10. package/Cargo.lock +908 -0
  11. package/Cargo.toml +32 -0
  12. package/LICENSE +21 -0
  13. package/README.md +230 -0
  14. package/bin/ryl.js +1 -0
  15. package/clippy.toml +1 -0
  16. package/docs/config-presets.md +100 -0
  17. package/img/benchmark-5x5-5runs.svg +2176 -0
  18. package/package.json +28 -0
  19. package/pyproject.toml +42 -0
  20. package/ruff.toml +107 -0
  21. package/rumdl.toml +20 -0
  22. package/rust-toolchain.toml +3 -0
  23. package/rustfmt.toml +3 -0
  24. package/scripts/benchmark_perf_vs_yamllint.py +400 -0
  25. package/scripts/coverage-missing.ps1 +80 -0
  26. package/scripts/coverage-missing.sh +60 -0
  27. package/src/bin/discover_config_bin.rs +24 -0
  28. package/src/cli_support.rs +33 -0
  29. package/src/conf/mod.rs +85 -0
  30. package/src/config.rs +2099 -0
  31. package/src/decoder.rs +326 -0
  32. package/src/discover.rs +31 -0
  33. package/src/lib.rs +19 -0
  34. package/src/lint.rs +558 -0
  35. package/src/main.rs +535 -0
  36. package/src/migrate.rs +233 -0
  37. package/src/rules/anchors.rs +517 -0
  38. package/src/rules/braces.rs +77 -0
  39. package/src/rules/brackets.rs +77 -0
  40. package/src/rules/colons.rs +475 -0
  41. package/src/rules/commas.rs +372 -0
  42. package/src/rules/comments.rs +299 -0
  43. package/src/rules/comments_indentation.rs +243 -0
  44. package/src/rules/document_end.rs +175 -0
  45. package/src/rules/document_start.rs +84 -0
  46. package/src/rules/empty_lines.rs +152 -0
  47. package/src/rules/empty_values.rs +255 -0
  48. package/src/rules/float_values.rs +259 -0
  49. package/src/rules/flow_collection.rs +562 -0
  50. package/src/rules/hyphens.rs +104 -0
  51. package/src/rules/indentation.rs +803 -0
  52. package/src/rules/key_duplicates.rs +218 -0
  53. package/src/rules/key_ordering.rs +303 -0
  54. package/src/rules/line_length.rs +326 -0
  55. package/src/rules/mod.rs +25 -0
  56. package/src/rules/new_line_at_end_of_file.rs +23 -0
  57. package/src/rules/new_lines.rs +95 -0
  58. package/src/rules/octal_values.rs +121 -0
  59. package/src/rules/quoted_strings.rs +577 -0
  60. package/src/rules/span_utils.rs +37 -0
  61. package/src/rules/trailing_spaces.rs +65 -0
  62. package/src/rules/truthy.rs +420 -0
  63. package/tests/brackets_carriage_return.rs +114 -0
  64. package/tests/build_global_cfg_error.rs +23 -0
  65. package/tests/cli_anchors_rule.rs +143 -0
  66. package/tests/cli_braces_rule.rs +104 -0
  67. package/tests/cli_brackets_rule.rs +104 -0
  68. package/tests/cli_colons_rule.rs +65 -0
  69. package/tests/cli_commas_rule.rs +104 -0
  70. package/tests/cli_comments_indentation_rule.rs +61 -0
  71. package/tests/cli_comments_rule.rs +67 -0
  72. package/tests/cli_config_data_error.rs +30 -0
  73. package/tests/cli_config_flags.rs +66 -0
  74. package/tests/cli_config_migrate.rs +229 -0
  75. package/tests/cli_document_end_rule.rs +92 -0
  76. package/tests/cli_document_start_rule.rs +92 -0
  77. package/tests/cli_empty_lines_rule.rs +87 -0
  78. package/tests/cli_empty_values_rule.rs +68 -0
  79. package/tests/cli_env_config.rs +34 -0
  80. package/tests/cli_exit_and_errors.rs +41 -0
  81. package/tests/cli_file_encoding.rs +203 -0
  82. package/tests/cli_float_values_rule.rs +64 -0
  83. package/tests/cli_format_options.rs +316 -0
  84. package/tests/cli_global_cfg_relaxed.rs +20 -0
  85. package/tests/cli_hyphens_rule.rs +104 -0
  86. package/tests/cli_indentation_rule.rs +65 -0
  87. package/tests/cli_invalid_project_config.rs +39 -0
  88. package/tests/cli_key_duplicates_rule.rs +104 -0
  89. package/tests/cli_key_ordering_rule.rs +59 -0
  90. package/tests/cli_line_length_rule.rs +85 -0
  91. package/tests/cli_list_files.rs +29 -0
  92. package/tests/cli_new_line_rule.rs +141 -0
  93. package/tests/cli_new_lines_rule.rs +119 -0
  94. package/tests/cli_octal_values_rule.rs +60 -0
  95. package/tests/cli_quoted_strings_rule.rs +47 -0
  96. package/tests/cli_toml_config.rs +119 -0
  97. package/tests/cli_trailing_spaces_rule.rs +77 -0
  98. package/tests/cli_truthy_rule.rs +83 -0
  99. package/tests/cli_yaml_files_negation.rs +45 -0
  100. package/tests/colons_rule.rs +303 -0
  101. package/tests/common/compat.rs +114 -0
  102. package/tests/common/fake_env.rs +93 -0
  103. package/tests/common/mod.rs +1 -0
  104. package/tests/conf_builtin.rs +9 -0
  105. package/tests/config_anchors.rs +84 -0
  106. package/tests/config_braces.rs +121 -0
  107. package/tests/config_brackets.rs +127 -0
  108. package/tests/config_commas.rs +79 -0
  109. package/tests/config_comments.rs +65 -0
  110. package/tests/config_comments_indentation.rs +20 -0
  111. package/tests/config_deep_merge_nonstring_key.rs +24 -0
  112. package/tests/config_document_end.rs +54 -0
  113. package/tests/config_document_start.rs +55 -0
  114. package/tests/config_empty_lines.rs +48 -0
  115. package/tests/config_empty_values.rs +35 -0
  116. package/tests/config_env_errors.rs +23 -0
  117. package/tests/config_env_invalid_inline.rs +15 -0
  118. package/tests/config_env_missing.rs +63 -0
  119. package/tests/config_env_shim.rs +301 -0
  120. package/tests/config_explicit_file_parse_error.rs +55 -0
  121. package/tests/config_extended_features.rs +225 -0
  122. package/tests/config_extends_inline.rs +185 -0
  123. package/tests/config_extends_sequence.rs +18 -0
  124. package/tests/config_find_project_home_boundary.rs +54 -0
  125. package/tests/config_find_project_two_files_in_cwd.rs +47 -0
  126. package/tests/config_float_values.rs +34 -0
  127. package/tests/config_from_yaml_paths.rs +32 -0
  128. package/tests/config_hyphens.rs +51 -0
  129. package/tests/config_ignore_errors.rs +243 -0
  130. package/tests/config_ignore_overrides.rs +83 -0
  131. package/tests/config_indentation.rs +65 -0
  132. package/tests/config_invalid_globs.rs +16 -0
  133. package/tests/config_invalid_types.rs +19 -0
  134. package/tests/config_key_duplicates.rs +34 -0
  135. package/tests/config_key_ordering.rs +70 -0
  136. package/tests/config_line_length.rs +65 -0
  137. package/tests/config_locale.rs +111 -0
  138. package/tests/config_merge.rs +26 -0
  139. package/tests/config_new_lines.rs +89 -0
  140. package/tests/config_octal_values.rs +33 -0
  141. package/tests/config_quoted_strings.rs +195 -0
  142. package/tests/config_rule_level.rs +147 -0
  143. package/tests/config_rules_non_string_keys.rs +23 -0
  144. package/tests/config_scalar_overrides.rs +27 -0
  145. package/tests/config_to_toml.rs +110 -0
  146. package/tests/config_toml_coverage.rs +80 -0
  147. package/tests/config_toml_discovery.rs +304 -0
  148. package/tests/config_trailing_spaces.rs +152 -0
  149. package/tests/config_truthy.rs +77 -0
  150. package/tests/config_yaml_files.rs +62 -0
  151. package/tests/config_yaml_files_all_non_string.rs +15 -0
  152. package/tests/config_yaml_files_empty.rs +30 -0
  153. package/tests/coverage_commas.rs +46 -0
  154. package/tests/decoder_decode.rs +338 -0
  155. package/tests/discover_config_bin_all.rs +66 -0
  156. package/tests/discover_config_bin_env_invalid_yaml.rs +26 -0
  157. package/tests/discover_config_bin_project_config_parse_error.rs +24 -0
  158. package/tests/discover_config_bin_user_global_error.rs +26 -0
  159. package/tests/discover_module.rs +30 -0
  160. package/tests/discover_per_file_dir.rs +10 -0
  161. package/tests/discover_per_file_project_config_error.rs +21 -0
  162. package/tests/float_values.rs +43 -0
  163. package/tests/lint_multi_errors.rs +32 -0
  164. package/tests/main_yaml_ok_filtering.rs +30 -0
  165. package/tests/migrate_module.rs +259 -0
  166. package/tests/resolve_ctx_empty_parent.rs +16 -0
  167. package/tests/rule_anchors.rs +442 -0
  168. package/tests/rule_braces.rs +258 -0
  169. package/tests/rule_brackets.rs +217 -0
  170. package/tests/rule_commas.rs +205 -0
  171. package/tests/rule_comments.rs +197 -0
  172. package/tests/rule_comments_indentation.rs +127 -0
  173. package/tests/rule_document_end.rs +118 -0
  174. package/tests/rule_document_start.rs +60 -0
  175. package/tests/rule_empty_lines.rs +96 -0
  176. package/tests/rule_empty_values.rs +102 -0
  177. package/tests/rule_float_values.rs +109 -0
  178. package/tests/rule_hyphens.rs +65 -0
  179. package/tests/rule_indentation.rs +455 -0
  180. package/tests/rule_key_duplicates.rs +76 -0
  181. package/tests/rule_key_ordering.rs +207 -0
  182. package/tests/rule_line_length.rs +200 -0
  183. package/tests/rule_new_lines.rs +51 -0
  184. package/tests/rule_octal_values.rs +53 -0
  185. package/tests/rule_quoted_strings.rs +290 -0
  186. package/tests/rule_trailing_spaces.rs +41 -0
  187. package/tests/rule_truthy.rs +236 -0
  188. package/tests/user_global_invalid_yaml.rs +32 -0
  189. package/tests/yamllint_compat_anchors.rs +280 -0
  190. package/tests/yamllint_compat_braces.rs +411 -0
  191. package/tests/yamllint_compat_brackets.rs +364 -0
  192. package/tests/yamllint_compat_colons.rs +298 -0
  193. package/tests/yamllint_compat_colors.rs +80 -0
  194. package/tests/yamllint_compat_commas.rs +375 -0
  195. package/tests/yamllint_compat_comments.rs +167 -0
  196. package/tests/yamllint_compat_comments_indentation.rs +281 -0
  197. package/tests/yamllint_compat_config.rs +170 -0
  198. package/tests/yamllint_compat_document_end.rs +243 -0
  199. package/tests/yamllint_compat_document_start.rs +136 -0
  200. package/tests/yamllint_compat_empty_lines.rs +117 -0
  201. package/tests/yamllint_compat_empty_values.rs +179 -0
  202. package/tests/yamllint_compat_float_values.rs +216 -0
  203. package/tests/yamllint_compat_hyphens.rs +223 -0
  204. package/tests/yamllint_compat_indentation.rs +398 -0
  205. package/tests/yamllint_compat_key_duplicates.rs +139 -0
  206. package/tests/yamllint_compat_key_ordering.rs +170 -0
  207. package/tests/yamllint_compat_line_length.rs +375 -0
  208. package/tests/yamllint_compat_list.rs +127 -0
  209. package/tests/yamllint_compat_new_line.rs +133 -0
  210. package/tests/yamllint_compat_newline_types.rs +185 -0
  211. package/tests/yamllint_compat_octal_values.rs +172 -0
  212. package/tests/yamllint_compat_quoted_strings.rs +154 -0
  213. package/tests/yamllint_compat_syntax.rs +200 -0
  214. package/tests/yamllint_compat_trailing_spaces.rs +162 -0
  215. package/tests/yamllint_compat_truthy.rs +130 -0
  216. package/tests/yamllint_compat_yaml_files.rs +81 -0
  217. package/typos.toml +2 -0
@@ -0,0 +1,63 @@
1
+ use std::fs;
2
+
3
+ use ryl::config::{Env, Overrides, SystemEnv, discover_config_with_env};
4
+ use tempfile::tempdir;
5
+
6
+ #[test]
7
+ fn env_points_to_missing_file_is_ignored() {
8
+ let workspace = tempdir().unwrap();
9
+ let inputs = vec![workspace.path().join("input.yaml")];
10
+ let ctx = discover_config_with_env(&inputs, &Overrides::default(), &|k| {
11
+ if k == "YAMLLINT_CONFIG_FILE" {
12
+ Some("/tmp/this/does/not/exist.yml".into())
13
+ } else {
14
+ None
15
+ }
16
+ })
17
+ .expect("discover should succeed");
18
+ // Missing env config falls back to default preset
19
+ assert!(ctx.source.is_none());
20
+ assert!(ctx.config.rule_names().iter().any(|r| r == "anchors"));
21
+ }
22
+
23
+ #[test]
24
+ fn env_tilde_path_uses_closure_home_dir() {
25
+ let dir = tempdir().unwrap();
26
+ let config_path = dir
27
+ .path()
28
+ .join(".config")
29
+ .join("yamllint")
30
+ .join("custom.yml");
31
+ fs::create_dir_all(config_path.parent().unwrap()).unwrap();
32
+ fs::write(&config_path, "rules: {}\n").unwrap();
33
+
34
+ let project_root = dir.path().join("workspace");
35
+ fs::create_dir_all(&project_root).unwrap();
36
+ let inputs = vec![project_root.join("input.yaml")];
37
+ let ctx = discover_config_with_env(&inputs, &Overrides::default(), &|k| match k {
38
+ "YAMLLINT_CONFIG_FILE" => Some("~/.config/yamllint/custom.yml".into()),
39
+ "HOME" => Some(dir.path().to_str().unwrap().into()),
40
+ _ => None,
41
+ })
42
+ .expect("discover should succeed");
43
+
44
+ assert_eq!(ctx.source.as_deref(), Some(config_path.as_path()));
45
+ }
46
+
47
+ #[test]
48
+ fn env_tilde_path_without_home_falls_back_to_system_home() {
49
+ let workspace = tempdir().unwrap();
50
+ let inputs = vec![workspace.path().join("input.yaml")];
51
+ let _ = SystemEnv.home_dir();
52
+ let ctx = discover_config_with_env(&inputs, &Overrides::default(), &|k| {
53
+ if k == "YAMLLINT_CONFIG_FILE" {
54
+ Some("~/.config/yamllint/missing.yml".into())
55
+ } else {
56
+ None
57
+ }
58
+ })
59
+ .expect("discover should succeed");
60
+
61
+ // No config file found; default preset applies.
62
+ assert!(ctx.source.is_none());
63
+ }
@@ -0,0 +1,301 @@
1
+ use std::collections::{HashMap, HashSet};
2
+ use std::path::{Path, PathBuf};
3
+
4
+ use ryl::config::{
5
+ Env, Overrides, SystemEnv, discover_config_with, discover_per_file_with,
6
+ };
7
+ use tempfile::tempdir;
8
+
9
+ #[derive(Default)]
10
+ struct FakeEnv {
11
+ cwd: PathBuf,
12
+ cfg_dir: Option<PathBuf>,
13
+ files: HashMap<PathBuf, String>,
14
+ exists: HashSet<PathBuf>,
15
+ vars: HashMap<String, String>,
16
+ home: Option<PathBuf>,
17
+ }
18
+
19
+ impl FakeEnv {
20
+ fn with_cwd(mut self, p: impl Into<PathBuf>) -> Self {
21
+ self.cwd = p.into();
22
+ self
23
+ }
24
+ fn with_config_dir(mut self, p: impl Into<PathBuf>) -> Self {
25
+ self.cfg_dir = Some(p.into());
26
+ self
27
+ }
28
+ fn add_file(mut self, p: impl Into<PathBuf>, content: impl Into<String>) -> Self {
29
+ self.files.insert(p.into(), content.into());
30
+ self
31
+ }
32
+ fn add_exist(mut self, p: impl Into<PathBuf>) -> Self {
33
+ self.exists.insert(p.into());
34
+ self
35
+ }
36
+ fn set_var(mut self, k: impl Into<String>, v: impl Into<String>) -> Self {
37
+ self.vars.insert(k.into(), v.into());
38
+ self
39
+ }
40
+ fn with_home(mut self, p: impl Into<PathBuf>) -> Self {
41
+ self.home = Some(p.into());
42
+ self
43
+ }
44
+ }
45
+
46
+ impl Env for FakeEnv {
47
+ fn current_dir(&self) -> PathBuf {
48
+ self.cwd.clone()
49
+ }
50
+ fn config_dir(&self) -> Option<PathBuf> {
51
+ self.cfg_dir.clone()
52
+ }
53
+ fn read_to_string(&self, p: &Path) -> Result<String, String> {
54
+ self.files.get(p).cloned().ok_or_else(|| {
55
+ format!("failed to read config file {}: not found", p.display())
56
+ })
57
+ }
58
+ fn path_exists(&self, p: &Path) -> bool {
59
+ self.files.contains_key(p) || self.exists.contains(p)
60
+ }
61
+ fn env_var(&self, key: &str) -> Option<String> {
62
+ self.vars.get(key).cloned()
63
+ }
64
+ fn home_dir(&self) -> Option<PathBuf> {
65
+ self.home
66
+ .clone()
67
+ .or_else(|| self.vars.get("HOME").map(PathBuf::from))
68
+ .or_else(|| self.vars.get("USERPROFILE").map(PathBuf::from))
69
+ }
70
+ }
71
+
72
+ fn cfg_rules_empty() -> String {
73
+ "rules: {}\n".to_string()
74
+ }
75
+
76
+ #[test]
77
+ fn shim_inline_config_path() {
78
+ let env = FakeEnv::default().with_cwd("/home/user");
79
+ let ctx = discover_config_with(
80
+ &[],
81
+ &Overrides {
82
+ config_file: None,
83
+ config_data: Some(cfg_rules_empty()),
84
+ },
85
+ &env,
86
+ )
87
+ .unwrap();
88
+ assert!(ctx.config.rule_names().is_empty());
89
+ assert_eq!(ctx.base_dir, PathBuf::from("/home/user"));
90
+ assert!(ctx.source.is_none());
91
+ }
92
+
93
+ #[test]
94
+ fn shim_file_config_path_with_parent_none_uses_cwd() {
95
+ let env = FakeEnv::default()
96
+ .with_cwd("/work")
97
+ .add_file("", cfg_rules_empty());
98
+ let ctx = discover_config_with(
99
+ &[],
100
+ &Overrides {
101
+ config_file: Some(PathBuf::from("")),
102
+ config_data: None,
103
+ },
104
+ &env,
105
+ )
106
+ .unwrap();
107
+ assert_eq!(ctx.base_dir, PathBuf::from("/work"));
108
+ assert_eq!(ctx.source.unwrap(), PathBuf::from(""));
109
+ }
110
+
111
+ #[test]
112
+ fn shim_env_var_points_to_file_base_dir_from_cwd() {
113
+ let env = FakeEnv::default()
114
+ .with_cwd("/tmp/cwd")
115
+ .add_file("", cfg_rules_empty())
116
+ .set_var("YAMLLINT_CONFIG_FILE", "");
117
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
118
+ assert_eq!(ctx.base_dir, PathBuf::from("/tmp/cwd"));
119
+ assert_eq!(ctx.source.unwrap(), PathBuf::from(""));
120
+ }
121
+
122
+ #[test]
123
+ fn shim_env_var_with_tilde_expands_home() {
124
+ let env = FakeEnv::default()
125
+ .with_cwd("/tmp/cwd")
126
+ .with_home("/home/tester")
127
+ .set_var("YAMLLINT_CONFIG_FILE", "~/.config/yamllint/custom.yml")
128
+ .add_file(
129
+ "/home/tester/.config/yamllint/custom.yml",
130
+ cfg_rules_empty(),
131
+ );
132
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
133
+ assert_eq!(
134
+ ctx.source.unwrap(),
135
+ PathBuf::from("/home/tester/.config/yamllint/custom.yml")
136
+ );
137
+ }
138
+
139
+ #[test]
140
+ fn shim_env_var_tilde_alone_uses_home_directory() {
141
+ let env = FakeEnv::default()
142
+ .with_cwd("/tmp/cwd")
143
+ .with_home("/home/tester")
144
+ .set_var("YAMLLINT_CONFIG_FILE", "~")
145
+ .add_file("/home/tester", cfg_rules_empty());
146
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
147
+ assert_eq!(ctx.source.unwrap(), PathBuf::from("/home/tester"));
148
+ }
149
+
150
+ #[test]
151
+ fn shim_env_var_tilde_without_home_keeps_literal_path() {
152
+ let env = FakeEnv::default()
153
+ .with_cwd("/tmp/cwd")
154
+ .set_var("YAMLLINT_CONFIG_FILE", "~/.config/yamllint/custom.yml")
155
+ .add_file("~/.config/yamllint/custom.yml", cfg_rules_empty());
156
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
157
+ assert_eq!(
158
+ ctx.source.unwrap(),
159
+ PathBuf::from("~/.config/yamllint/custom.yml")
160
+ );
161
+ }
162
+
163
+ #[test]
164
+ fn system_env_home_dir_accessible() {
165
+ let env = SystemEnv;
166
+ let _ = env.home_dir();
167
+ }
168
+
169
+ #[test]
170
+ fn shim_env_var_userprofile_handles_windows_tilde_backslash() {
171
+ let env = FakeEnv::default()
172
+ .with_cwd("/tmp/cwd")
173
+ .set_var("USERPROFILE", "/profiles/user")
174
+ .set_var("YAMLLINT_CONFIG_FILE", "~\\config.yml")
175
+ .add_file("/profiles/user/config.yml", cfg_rules_empty());
176
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
177
+ assert_eq!(
178
+ ctx.source.unwrap(),
179
+ PathBuf::from("/profiles/user/config.yml")
180
+ );
181
+ }
182
+
183
+ #[test]
184
+ fn shim_project_config_discovered_from_inputs() {
185
+ let env = FakeEnv::default()
186
+ .with_cwd("/wd")
187
+ .add_file("/proj/.yamllint", cfg_rules_empty())
188
+ .add_exist("/proj/.yamllint")
189
+ .add_exist("/proj/file.yaml");
190
+ let ctx = discover_config_with(
191
+ &[PathBuf::from("/proj/file.yaml")],
192
+ &Overrides::default(),
193
+ &env,
194
+ )
195
+ .unwrap();
196
+ assert_eq!(ctx.base_dir, PathBuf::from("/proj"));
197
+ assert!(ctx.source.unwrap().ends_with(".yamllint"));
198
+ }
199
+
200
+ #[test]
201
+ fn shim_user_global_config_applies_when_no_project_or_env() {
202
+ let env = FakeEnv::default()
203
+ .with_cwd("/wd")
204
+ .with_config_dir("/xdg")
205
+ .add_file("/xdg/yamllint/config", cfg_rules_empty())
206
+ .add_exist("/xdg/yamllint/config");
207
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
208
+ assert!(ctx.source.unwrap().ends_with("yamllint/config"));
209
+ }
210
+
211
+ #[test]
212
+ fn shim_user_global_missing_falls_back_to_default() {
213
+ let env = FakeEnv::default()
214
+ .with_cwd("/wd")
215
+ .with_config_dir("/xdg-none");
216
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
217
+ assert!(ctx.source.is_none());
218
+ // default preset filters by extension
219
+ assert!(
220
+ ctx.config
221
+ .is_yaml_candidate(&PathBuf::from("x.yaml"), &ctx.base_dir)
222
+ );
223
+ }
224
+
225
+ #[test]
226
+ fn shim_systemenv_read_error_is_mapped() {
227
+ let env = SystemEnv;
228
+ let err = discover_config_with(
229
+ &[],
230
+ &Overrides {
231
+ config_file: Some(PathBuf::from("no_such_file.yml")),
232
+ config_data: None,
233
+ },
234
+ &env,
235
+ )
236
+ .unwrap_err();
237
+ assert!(err.contains("failed to read config file"));
238
+ }
239
+
240
+ #[test]
241
+ fn shim_systemenv_read_success_is_used() {
242
+ let td = tempfile::tempdir().unwrap();
243
+ let cfgp = td.path().join("ok.yml");
244
+ std::fs::write(&cfgp, "rules: {}\n").unwrap();
245
+ let env = SystemEnv;
246
+ let ctx = discover_config_with(
247
+ &[],
248
+ &Overrides {
249
+ config_file: Some(cfgp.clone()),
250
+ config_data: None,
251
+ },
252
+ &env,
253
+ )
254
+ .unwrap();
255
+ assert!(ctx.source.unwrap().ends_with("ok.yml"));
256
+ }
257
+
258
+ #[test]
259
+ fn system_env_read_invalid_encoding_reports_error() {
260
+ let td = tempdir().unwrap();
261
+ let path = td.path().join("bad.yml");
262
+ std::fs::write(&path, [0xFFu8, 0xFE, 0x00]).unwrap();
263
+ let env = SystemEnv;
264
+ let err = env.read_to_string(&path).unwrap_err();
265
+ assert!(err.contains("invalid"));
266
+ }
267
+
268
+ #[test]
269
+ fn shim_env_var_tilde_slash_only_returns_home() {
270
+ let env = FakeEnv::default()
271
+ .with_cwd("/tmp/cwd")
272
+ .set_var("HOME", "/home/tester")
273
+ .set_var("YAMLLINT_CONFIG_FILE", "~/")
274
+ .add_file("/home/tester", cfg_rules_empty());
275
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
276
+ assert_eq!(ctx.source.unwrap(), PathBuf::from("/home/tester"));
277
+ }
278
+
279
+ #[test]
280
+ fn shim_env_var_tilde_backslash_only_returns_home() {
281
+ let env = FakeEnv::default()
282
+ .with_cwd("/tmp/cwd")
283
+ .set_var("USERPROFILE", "/profiles/user")
284
+ .set_var("YAMLLINT_CONFIG_FILE", "~\\")
285
+ .add_file("/profiles/user", cfg_rules_empty());
286
+ let ctx = discover_config_with(&[], &Overrides::default(), &env).unwrap();
287
+ assert_eq!(ctx.source.unwrap(), PathBuf::from("/profiles/user"));
288
+ }
289
+
290
+ #[test]
291
+ fn shim_discover_per_file_uses_project_else_user_global_else_default() {
292
+ let env = FakeEnv::default()
293
+ .with_cwd("/wd")
294
+ .with_config_dir("/xdg")
295
+ .add_file("/xdg/yamllint/config", cfg_rules_empty())
296
+ .add_exist("/xdg/yamllint/config");
297
+ // No project config, so user-global applies
298
+ let file = PathBuf::from("/proj/no_config/file.yaml");
299
+ let ctx = discover_per_file_with(&file, &env).unwrap();
300
+ assert!(ctx.source.unwrap().ends_with("yamllint/config"));
301
+ }
@@ -0,0 +1,55 @@
1
+ use std::collections::{HashMap, HashSet};
2
+ use std::path::{Path, PathBuf};
3
+
4
+ struct FakeEnv {
5
+ exists: HashSet<PathBuf>,
6
+ files: HashMap<PathBuf, String>,
7
+ }
8
+
9
+ impl FakeEnv {
10
+ fn new() -> Self {
11
+ Self {
12
+ exists: HashSet::new(),
13
+ files: HashMap::new(),
14
+ }
15
+ }
16
+ }
17
+
18
+ impl ryl::config::Env for FakeEnv {
19
+ fn current_dir(&self) -> PathBuf {
20
+ PathBuf::from(".")
21
+ }
22
+ fn config_dir(&self) -> Option<PathBuf> {
23
+ None
24
+ }
25
+ fn read_to_string(&self, p: &Path) -> Result<String, String> {
26
+ self.files
27
+ .get(p)
28
+ .cloned()
29
+ .ok_or_else(|| format!("missing {}", p.display()))
30
+ }
31
+ fn path_exists(&self, p: &Path) -> bool {
32
+ self.exists.contains(p)
33
+ }
34
+ fn home_dir(&self) -> Option<PathBuf> {
35
+ None
36
+ }
37
+ fn env_var(&self, _key: &str) -> Option<String> {
38
+ None
39
+ }
40
+ }
41
+
42
+ #[test]
43
+ fn explicit_config_file_with_invalid_yaml_errors() {
44
+ let mut envx = FakeEnv::new();
45
+ let file = PathBuf::from("cfg.yml");
46
+ envx.exists.insert(file.clone());
47
+ envx.files.insert(file.clone(), "rules: {".into());
48
+
49
+ let overrides = ryl::config::Overrides {
50
+ config_file: Some(file),
51
+ config_data: None,
52
+ };
53
+ let res = ryl::config::discover_config_with(&[], &overrides, &envx);
54
+ assert!(res.is_err());
55
+ }
@@ -0,0 +1,225 @@
1
+ use ryl::config::{Overrides, discover_config};
2
+ use std::fs;
3
+ use tempfile::tempdir;
4
+
5
+ #[test]
6
+ fn ignore_block_supports_negation() {
7
+ let td = tempdir().unwrap();
8
+ let cfg = td.path().join("conf.yaml");
9
+ fs::write(
10
+ &cfg,
11
+ "ignore: |\n **/*.skip.yaml\n !**/keep.skip.yaml\n nested/\nyaml-files: []\nrules: {}\n",
12
+ )
13
+ .unwrap();
14
+
15
+ let ctx = discover_config(
16
+ &[],
17
+ &Overrides {
18
+ config_file: Some(cfg.clone()),
19
+ config_data: None,
20
+ },
21
+ )
22
+ .expect("config parse");
23
+ let base = ctx.base_dir.clone();
24
+
25
+ assert!(
26
+ ctx.config
27
+ .is_file_ignored(&td.path().join("a.skip.yaml"), &base)
28
+ );
29
+ assert!(
30
+ !ctx.config
31
+ .is_file_ignored(&td.path().join("keep.skip.yaml"), &base)
32
+ );
33
+ assert!(
34
+ ctx.config
35
+ .is_file_ignored(&td.path().join("nested/file.yaml"), &base)
36
+ );
37
+ }
38
+
39
+ #[test]
40
+ fn ignore_from_file_patterns_are_loaded() {
41
+ let td = tempdir().unwrap();
42
+ let cfg = td.path().join("conf.yaml");
43
+ let ignore = td.path().join(".yamllint-ignore");
44
+ fs::write(
45
+ &cfg,
46
+ "ignore-from-file: .yamllint-ignore\nyaml-files: []\nrules: {}\n",
47
+ )
48
+ .unwrap();
49
+ fs::write(&ignore, "*.gen.yaml\n!.keep.yaml\n").unwrap();
50
+
51
+ let ctx = discover_config(
52
+ &[],
53
+ &Overrides {
54
+ config_file: Some(cfg.clone()),
55
+ config_data: None,
56
+ },
57
+ )
58
+ .expect("config parse");
59
+ let base = ctx.base_dir.clone();
60
+
61
+ assert!(
62
+ ctx.config
63
+ .is_file_ignored(&td.path().join("output.gen.yaml"), &base)
64
+ );
65
+ assert!(
66
+ !ctx.config
67
+ .is_file_ignored(&td.path().join(".keep.yaml"), &base)
68
+ );
69
+ assert!(
70
+ ctx.config
71
+ .ignore_patterns()
72
+ .iter()
73
+ .any(|p| p == "*.gen.yaml")
74
+ );
75
+ }
76
+
77
+ #[test]
78
+ fn ignore_directory_pattern_allows_reinclude_of_specific_file() {
79
+ let td = tempdir().unwrap();
80
+ let cfg = td.path().join("conf.yaml");
81
+ fs::write(
82
+ &cfg,
83
+ "ignore: |\n build/\n !build/keep.yaml\nyaml-files: []\nrules: {}\n",
84
+ )
85
+ .unwrap();
86
+
87
+ let ctx = discover_config(
88
+ &[],
89
+ &Overrides {
90
+ config_file: Some(cfg.clone()),
91
+ config_data: None,
92
+ },
93
+ )
94
+ .expect("config parse");
95
+ let base = ctx.base_dir.clone();
96
+
97
+ assert!(
98
+ ctx.config
99
+ .is_file_ignored(&td.path().join("build/drop.yaml"), &base)
100
+ );
101
+ assert!(
102
+ !ctx.config
103
+ .is_file_ignored(&td.path().join("build/keep.yaml"), &base)
104
+ );
105
+ }
106
+
107
+ #[test]
108
+ fn ignore_from_file_directory_pattern_allows_reinclude_of_specific_file() {
109
+ let td = tempdir().unwrap();
110
+ let cfg = td.path().join("conf.yaml");
111
+ let ignore = td.path().join(".yamllint-ignore");
112
+ fs::write(
113
+ &cfg,
114
+ "ignore-from-file: .yamllint-ignore\nyaml-files: []\nrules: {}\n",
115
+ )
116
+ .unwrap();
117
+ fs::write(&ignore, "build/\n!build/keep.yaml\n").unwrap();
118
+
119
+ let ctx = discover_config(
120
+ &[],
121
+ &Overrides {
122
+ config_file: Some(cfg.clone()),
123
+ config_data: None,
124
+ },
125
+ )
126
+ .expect("config parse");
127
+ let base = ctx.base_dir.clone();
128
+
129
+ assert!(
130
+ ctx.config
131
+ .is_file_ignored(&td.path().join("build/drop.yaml"), &base)
132
+ );
133
+ assert!(
134
+ !ctx.config
135
+ .is_file_ignored(&td.path().join("build/keep.yaml"), &base)
136
+ );
137
+ }
138
+
139
+ #[test]
140
+ fn rule_ignore_handles_direct_ignore_whitelist_and_parent_fallback() {
141
+ let td = tempdir().unwrap();
142
+ let cfg = td.path().join("conf.yaml");
143
+ fs::write(
144
+ &cfg,
145
+ "yaml-files: []\nrules:\n trailing-spaces:\n ignore: |\n build/\n !build/keep.yaml\n drop.yaml\n",
146
+ )
147
+ .unwrap();
148
+
149
+ let ctx = discover_config(
150
+ &[],
151
+ &Overrides {
152
+ config_file: Some(cfg.clone()),
153
+ config_data: None,
154
+ },
155
+ )
156
+ .expect("config parse");
157
+ let base = ctx.base_dir.clone();
158
+
159
+ assert!(!ctx.config.is_rule_ignored(
160
+ "trailing-spaces",
161
+ &td.path().join("build/keep.yaml"),
162
+ &base
163
+ ));
164
+ assert!(ctx.config.is_rule_ignored(
165
+ "trailing-spaces",
166
+ &td.path().join("drop.yaml"),
167
+ &base
168
+ ));
169
+ assert!(ctx.config.is_rule_ignored(
170
+ "trailing-spaces",
171
+ &td.path().join("build/sub/nested.yaml"),
172
+ &base,
173
+ ));
174
+ }
175
+
176
+ #[test]
177
+ fn extends_resolves_relative_file_paths() {
178
+ let td = tempdir().unwrap();
179
+ let base_cfg = td.path().join("base.yaml");
180
+ fs::write(
181
+ &base_cfg,
182
+ "ignore: ['base/**']\nyaml-files: []\nrules: {}\n",
183
+ )
184
+ .unwrap();
185
+ let child_cfg = td.path().join("child.yaml");
186
+ fs::write(
187
+ &child_cfg,
188
+ "extends: base.yaml\nignore: ['child/**']\nyaml-files: []\nrules: {}\n",
189
+ )
190
+ .unwrap();
191
+
192
+ let ctx = discover_config(
193
+ &[],
194
+ &Overrides {
195
+ config_file: Some(child_cfg.clone()),
196
+ config_data: None,
197
+ },
198
+ )
199
+ .expect("config parse");
200
+ let base = ctx.base_dir.clone();
201
+
202
+ assert!(
203
+ !ctx.config
204
+ .is_file_ignored(&td.path().join("base/one.yaml"), &base),
205
+ "parent ignore entries should be replaced by child overrides"
206
+ );
207
+ assert!(
208
+ ctx.config
209
+ .is_file_ignored(&td.path().join("child/two.yaml"), &base)
210
+ );
211
+ }
212
+
213
+ #[test]
214
+ fn config_data_builtin_name_expands() {
215
+ let ctx = discover_config(
216
+ &[],
217
+ &Overrides {
218
+ config_file: None,
219
+ config_data: Some("extends: relaxed".to_string()),
220
+ },
221
+ )
222
+ .expect("config parse");
223
+
224
+ assert!(ctx.config.rule_names().iter().any(|r| r == "braces"));
225
+ }