Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 86728a0

Browse files
committed
tests: show issue 27
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 0eee83a commit 86728a0

File tree

6 files changed

+100
-8
lines changed

6 files changed

+100
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/src/build_system.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::helpers::pep508::{format_requirement, get_canonic_requirement_name};
33
use crate::helpers::table::{for_entries, reorder_table_keys, Tables};
44

55
pub fn fix(tables: &mut Tables, keep_full_version: bool) {
6-
let table_element = tables.get(&String::from("build-system"));
6+
let table_element = tables.get("build-system");
77
if table_element.is_none() {
88
return;
99
}
@@ -107,6 +107,25 @@ mod tests {
107107
[[build-system.a]] # empty table within the array
108108
[[build-system.a]]
109109
name = "Nail"
110+
"#},
111+
false
112+
)]
113+
#[case::reorder(
114+
indoc ! {r#"
115+
[build-system]
116+
requires = [
117+
"hatchling",
118+
]
119+
build-backend = "hatchling.build"
120+
121+
"#},
122+
indoc ! {r#"
123+
[build-system]
124+
build-backend = "hatchling.build"
125+
requires = [
126+
"hatchling",
127+
]
128+
110129
"#},
111130
false
112131
)]

rust/src/helpers/table.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use std::iter::zip;
44
use std::ops::Index;
55

6-
use taplo::syntax::SyntaxKind::{ENTRY, IDENT, KEY, NEWLINE, TABLE_ARRAY_HEADER, TABLE_HEADER, VALUE};
6+
use taplo::syntax::SyntaxKind::{COMMA, ENTRY, IDENT, KEY, NEWLINE, TABLE_ARRAY_HEADER, TABLE_HEADER, VALUE};
77
use taplo::syntax::{SyntaxElement, SyntaxNode};
88
use taplo::HashSet;
99

@@ -154,8 +154,24 @@ fn get_key(k: &str) -> String {
154154
String::from(k)
155155
}
156156

157+
pub fn table_active_size(table: &[SyntaxElement]) -> usize {
158+
table
159+
.iter()
160+
.enumerate()
161+
.rev()
162+
.find_map(|(i, e)| {
163+
if e.kind() == VALUE || e.kind() == COMMA {
164+
Some(i+1)
165+
} else {
166+
None
167+
}
168+
})
169+
.unwrap_or(table.len())
170+
}
171+
157172
pub fn reorder_table_keys(table: &mut RefMut<Vec<SyntaxElement>>, order: &[&str]) {
158-
let (size, mut to_insert) = (table.len(), Vec::<SyntaxElement>::new());
173+
let size = table_active_size(table);
174+
let mut to_insert = Vec::<SyntaxElement>::new();
159175
let (key_to_position, key_set) = load_keys(table);
160176
let mut handled_positions = HashSet::<usize>::new();
161177
for current_key in order {
@@ -186,7 +202,7 @@ pub fn reorder_table_keys(table: &mut RefMut<Vec<SyntaxElement>>, order: &[&str]
186202
table.splice(0..size, to_insert);
187203
}
188204

189-
fn load_keys(table: &RefMut<Vec<SyntaxElement>>) -> (HashMap<String, usize>, Vec<Vec<SyntaxElement>>) {
205+
fn load_keys(table: &[SyntaxElement]) -> (HashMap<String, usize>, Vec<Vec<SyntaxElement>>) {
190206
let mut key_to_pos = HashMap::<String, usize>::new();
191207
let mut key_set = Vec::<Vec<SyntaxElement>>::new();
192208
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
@@ -238,12 +254,13 @@ pub fn get_table_name(entry: &SyntaxElement) -> String {
238254
String::new()
239255
}
240256

241-
pub fn for_entries<F>(table: &RefMut<Vec<SyntaxElement>>, f: &mut F)
257+
pub fn for_entries<F>(table: &[SyntaxElement], f: &mut F)
242258
where
243259
F: FnMut(String, &SyntaxNode),
244260
{
261+
let size = table_active_size(table);
245262
let mut key = String::new();
246-
for table_entry in table.iter() {
263+
for table_entry in table.iter().take(size) {
247264
if table_entry.kind() == ENTRY {
248265
for entry in table_entry.as_node().unwrap().children_with_tokens() {
249266
if entry.kind() == KEY {
@@ -316,3 +333,24 @@ pub fn collapse_sub_tables(tables: &mut Tables, name: &str) {
316333
sub.clear();
317334
}
318335
}
336+
337+
#[cfg(test)]
338+
mod tests {
339+
use super::*;
340+
341+
use taplo::parser::parse;
342+
343+
#[test]
344+
fn test_reorder() {
345+
let root_ast = parse("[A]\nb = 1\na = 1\n\n[B]\nb = 2")
346+
.into_syntax()
347+
.clone_for_update();
348+
let mut tables = Tables::from_ast(&root_ast);
349+
{
350+
let table = &mut tables.get("A").unwrap().first().unwrap().borrow_mut();
351+
reorder_table_keys(table, &["", "a", "b"]);
352+
}
353+
tables.reorder(&root_ast, &["B", "A"]);
354+
assert_eq!(root_ast.to_string(), "[B]\nb = 2\n\n[A]\na = 1\n\nb = 1\n\n");
355+
}
356+
}

rust/src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,29 @@ mod tests {
272272
true,
273273
(3, 8)
274274
)]
275+
#[case::space_issue_27(
276+
indoc ! {r#"
277+
[build-system]
278+
requires = [
279+
"hatchling",
280+
]
281+
build-backend = "hatchling.build"
282+
283+
[project]
284+
"#},
285+
indoc ! {r#"
286+
[build-system]
287+
build-backend = "hatchling.build"
288+
requires = [
289+
"hatchling",
290+
]
291+
292+
[project]
293+
"#},
294+
2,
295+
true,
296+
(3, 8)
297+
)]
275298
fn test_format_toml(
276299
#[case] start: &str,
277300
#[case] expected: &str,

rust/src/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fn generate_classifiers_to_entry(
291291
type MaxMinPythonWithClassifier = ((u8, u8), (u8, u8), Vec<u8>, Option<HashSet<String>>);
292292

293293
fn get_python_requires_with_classifier(
294-
table: &RefMut<Vec<SyntaxElement>>,
294+
table: &mut [SyntaxElement],
295295
max_supported_python: (u8, u8),
296296
min_supported_python: (u8, u8),
297297
) -> MaxMinPythonWithClassifier {

tests/test_main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
def test_format_toml() -> None:
99
txt = """
10+
[build-system]
11+
requires = [
12+
"hatchling",
13+
]
14+
build-backend = "hatchling.build"
15+
1016
[project]
1117
keywords = [
1218
"A",
@@ -32,6 +38,12 @@ def test_format_toml() -> None:
3238
res = format_toml(dedent(txt), settings)
3339

3440
expected = """\
41+
[build-system]
42+
build-backend = "hatchling.build"
43+
requires = [
44+
"hatchling",
45+
]
46+
3547
[project]
3648
keywords = [
3749
"A",

0 commit comments

Comments
 (0)