Skip to content

Commit abb8483

Browse files
committed
Parse testing inputs
1 parent 60d3ca5 commit abb8483

5 files changed

Lines changed: 130 additions & 76 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crossbeam = "0.8.4"
1111
dashmap = "6.1.0"
1212
expect-test = "1.5.1"
1313
futures = "0.3.31"
14+
indoc = "2.0.6"
1415
itertools = "0.14.0"
1516
lazy-regex = "3.4.1"
1617
lazy_static = "1.5.0"

src/completion.rs

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,8 @@ mod test {
165165
use itertools::Itertools;
166166

167167
use crate::{
168-
evaluator::{Evaluator, LockFlakeRequest},
168+
evaluator::Evaluator,
169169
file_types::{FileInfo, FileType},
170-
flakes::get_flake_inputs,
171-
safe_stringification::safe_stringify_flake,
172-
syntax::parse,
173170
};
174171

175172
use super::complete;
@@ -211,47 +208,9 @@ mod test {
211208
.await;
212209
}
213210

214-
async fn check_complete_flake(source: &str, _old_lock_file: Option<&str>, expected: Expect) {
215-
let (left, right) = source.split("$0").collect_tuple().unwrap();
216-
let offset = left.len() as u32;
217-
218-
let mut evaluator = Evaluator::new().await;
219-
220-
let source = format!("{}{}", left, right);
221-
222-
let lock_file = evaluator
223-
.lock_flake(&LockFlakeRequest {
224-
expression: safe_stringify_flake(
225-
parse(&source).expr().as_ref(),
226-
"/test/path".as_ref(),
227-
),
228-
old_lock_file: None,
229-
})
230-
.await
231-
.unwrap()
232-
.lock_file;
233-
234-
let inputs = get_flake_inputs(&mut evaluator, &lock_file).await.unwrap();
235-
236-
let actual = complete(
237-
&source,
238-
&FileInfo {
239-
file_type: FileType::Flake {
240-
locked: crate::file_types::LockedFlake::Locked { lock_file, inputs },
241-
},
242-
path: "/test/path/whatever.nix".into(),
243-
},
244-
offset,
245-
&mut evaluator,
246-
)
247-
.await
248-
.unwrap()
249-
.iter()
250-
.map(|item| item.label.clone())
251-
.collect_vec();
252-
253-
expected.assert_debug_eq(&actual);
254-
}
211+
// async fn check_complete_files(input: &str, expected: Expect) {
212+
// // let input = par
213+
// }
255214

256215
#[test_log::test(tokio::test)]
257216
async fn test_complete_let_in() {
@@ -936,37 +895,6 @@ mod test {
936895
.await;
937896
}
938897

939-
#[test_log::test(tokio::test)]
940-
async fn test_complete_flake_inputs() {
941-
check_complete_flake(
942-
r#"
943-
{
944-
inputs.flake-utils.url = "github:numtide/flake-utils?rev=919d646de7be200f3bf08cb76ae1f09402b6f9b4";
945-
outputs = {flake-utils}: flake-utils.lib.$0
946-
}
947-
"#,
948-
None,
949-
expect![[r#"
950-
[
951-
"allSystems",
952-
"check-utils",
953-
"defaultSystems",
954-
"eachDefaultSystem",
955-
"eachDefaultSystemMap",
956-
"eachSystem",
957-
"eachSystemMap",
958-
"filterPackages",
959-
"flattenTree",
960-
"meld",
961-
"mkApp",
962-
"simpleFlake",
963-
"system",
964-
]
965-
"#]],
966-
)
967-
.await;
968-
}
969-
970898
#[test_log::test(tokio::test)]
971899
async fn test_complete_system() {
972900
check_complete(

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod modules;
1515
mod safe_stringification;
1616
mod schema;
1717
mod syntax;
18+
mod testing;
1819
mod walk_attrs;
1920

2021
use anyhow::Result;

src/testing.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use std::{
2+
collections::BTreeMap,
3+
path::{Path, PathBuf},
4+
};
5+
6+
use ropey::Rope;
7+
8+
use crate::syntax::rope_offset_to_position;
9+
10+
#[derive(Debug)]
11+
pub struct Location {
12+
pub path: PathBuf,
13+
pub line: u32,
14+
pub col: u32,
15+
}
16+
17+
#[derive(Debug)]
18+
pub struct TestInput {
19+
pub files: BTreeMap<PathBuf, String>,
20+
pub location: Option<Location>,
21+
}
22+
23+
pub fn parse_test_input(input: &str) -> TestInput {
24+
let mut files = BTreeMap::new();
25+
let mut lines = input.lines().peekable();
26+
let mut current_path = PathBuf::from("/nowhere.nix");
27+
let mut current_content = Vec::new();
28+
let mut location = None;
29+
30+
let mut add_file = |current_path: &Path, current_content: &[String]| {
31+
if !current_content.is_empty() {
32+
let mut content = current_content.join("\n");
33+
if let Some(offset) = content.find("$0") {
34+
content = format!("{}{}", &content[0..offset], &content[offset + 2..]);
35+
let rope = Rope::from_str(&content);
36+
let position = rope_offset_to_position(&rope, offset);
37+
location = Some(Location {
38+
path: current_path.to_path_buf(),
39+
line: position.line,
40+
col: position.character,
41+
})
42+
}
43+
files.insert(current_path.to_path_buf(), content);
44+
}
45+
};
46+
47+
while let Some(line) = lines.next() {
48+
eprintln!("LINE = {:?}", line);
49+
if line.starts_with("## ") {
50+
add_file(&current_path, &current_content);
51+
current_content.clear();
52+
current_path = PathBuf::from(line[2..].trim());
53+
} else {
54+
current_content.push(line.to_string());
55+
}
56+
}
57+
58+
add_file(&current_path, &current_content);
59+
60+
TestInput { files, location }
61+
}
62+
63+
// pub async fn create_test_analyzer(input: &TestInput) {
64+
// let evaluator = Evaluator::new().await;
65+
// let (fetcher_input_send, fetcher_input_recv) = crossbeam::channel::unbounded::<FetcherInput>();
66+
// let (fetcher_output_send, fetcher_output_recv) =
67+
// crossbeam::channel::unbounded::<FetcherOutput>();
68+
69+
// let mut analyzer = Analyzer::new(evaluator, fetcher_input_send, fetcher_output_recv);
70+
71+
// for (path, content) in input.files.iter() {
72+
// analyzer.change_file(&path, &content);
73+
// }
74+
75+
// }
76+
77+
#[cfg(test)]
78+
mod test {
79+
use expect_test::{expect, Expect};
80+
use indoc::indoc;
81+
82+
use super::parse_test_input;
83+
84+
fn check_parse_test_input(input: &str, expected: Expect) {
85+
expected.assert_debug_eq(&parse_test_input(input.trim()));
86+
}
87+
88+
#[test]
89+
fn test_parse_test_input() {
90+
check_parse_test_input(
91+
indoc! {r#"
92+
## /test.nix
93+
aaaaaaaaaaaaaaa
94+
bbbbbb
95+
cccccccc
96+
## /your mother.nix
97+
aaaaaaaaaaaaaaaaaaaazzzzzzzzzzzzz
98+
xyz.$0cyz
99+
"# },
100+
expect![[r#"
101+
TestInput {
102+
files: {
103+
"/test.nix": "aaaaaaaaaaaaaaa\nbbbbbb\ncccccccc",
104+
"/your mother.nix": "aaaaaaaaaaaaaaaaaaaazzzzzzzzzzzzz\nxyz.cyz",
105+
},
106+
location: Some(
107+
Location {
108+
path: "/your mother.nix",
109+
line: 1,
110+
col: 4,
111+
},
112+
),
113+
}
114+
"#]],
115+
);
116+
}
117+
}

0 commit comments

Comments
 (0)