use super::*;
#[test]
fn sarif_log_opens_as_the_results_viewer() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
let view = app
.editor
.sarif
.as_ref()
.expect(".sarif opens in the viewer");
assert_eq!(view.entries.len(), 1);
assert_eq!(view.entries[0].file, "src/a.rs");
assert!(
app.editor.has_non_text_view(),
"a save must not write the stub"
);
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(140, 30)).unwrap();
term.draw(|f| app.render(f)).unwrap();
let screen = screen_text(&term);
assert!(
screen.contains("src/a.rs"),
"group header painted:\n{screen}"
);
assert!(
screen.contains("Query built from user input."),
"result row painted:\n{screen}"
);
assert!(screen.contains("Locations"), "tab strip painted:\n{screen}");
assert!(
screen.contains("R1 · Taint"),
"details pane shows the first result:\n{screen}"
);
}
#[test]
fn sarif_enter_opens_the_location_and_keeps_the_viewer_tab() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
let tabs_before = app.editor.tab_count();
app.handle_sarif_key(key(KeyCode::Down, KeyModifiers::NONE));
app.handle_sarif_key(key(KeyCode::Enter, KeyModifiers::NONE));
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("src/a.rs").as_path()),
"the location opened: {}",
app.status
);
assert_eq!((app.editor.cursor_row, app.editor.cursor_col), (1, 8));
assert_eq!(
app.editor.tab_count(),
tabs_before + 1,
"the viewer was pinned, not replaced by the preview open"
);
}
#[test]
fn sarif_space_previews_the_code_around_the_result_without_leaving_the_list() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
app.handle_sarif_key(key(KeyCode::Down, KeyModifiers::NONE));
app.handle_sarif_key(key(KeyCode::Char(' '), KeyModifiers::NONE));
app.refresh_sarif_preview();
let view = app.editor.sarif.as_ref().expect("still on the viewer");
let p = view.current_preview().expect("a preview").clone();
assert!(p.title.ends_with("a.rs"), "{}", p.title);
assert_eq!((p.first, p.target), (1, 2));
assert_eq!(p.lines[1], " let q = format!(\"{}\", id);");
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(140, 30)).unwrap();
term.draw(|f| app.render(f)).unwrap();
let screen = screen_text(&term);
assert!(screen.contains("Preview · "), "{screen}");
assert!(screen.contains("2 let q = format!"), "{screen}");
app.handle_sarif_key(key(KeyCode::Up, KeyModifiers::NONE));
app.refresh_sarif_preview();
assert!(
app.editor
.sarif
.as_ref()
.unwrap()
.current_preview()
.is_none()
);
app.handle_sarif_key(key(KeyCode::Down, KeyModifiers::NONE));
app.refresh_sarif_preview();
assert!(
app.editor
.sarif
.as_ref()
.unwrap()
.current_preview()
.is_some()
);
app.handle_sarif_key(key(KeyCode::Char(' '), KeyModifiers::NONE));
app.refresh_sarif_preview();
let view = app.editor.sarif.as_ref().unwrap();
assert!(!view.previewing && view.current_preview().is_none());
}
#[test]
fn a_sarif_result_whose_file_is_only_in_the_log_opens_the_logs_copy() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let log = tmp.path().join("embedded.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"T"}},
"artifacts":[
{"location":{"uri":"file:///build/gen/query.c"},"contents":{"text":"line one\nline two\nint bad;\n"}},
{"location":{"uri":"file:///build/blob.bin"},"contents":{"binary":"AAEC/w=="}}],
"results":[
{"ruleId":"R1","message":{"text":"text"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///build/gen/query.c","index":0},"region":{"startLine":3,"startColumn":5}}}]},
{"ruleId":"R2","message":{"text":"bin"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///build/blob.bin","index":1}}}]}]}]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
let select = |app: &mut App, rule: &str| {
let view = app.editor.sarif.as_mut().unwrap();
let rows = view.rows();
view.selected = rows
.iter()
.position(|r| matches!(r, crate::sarif::view::Row::Item { entry } if view.entries[*entry].rule_id == rule))
.unwrap();
};
select(&mut app, "R1");
app.open_selected_sarif_result();
let opened = app.editor.path.clone().expect("a copy opened");
assert!(
opened.starts_with(croft_cache_dir()),
"{opened:?}: {}",
app.status
);
assert_eq!(opened.file_name().unwrap(), "query.c");
assert_eq!(app.editor.lines[2], "int bad;");
assert_eq!((app.editor.cursor_row, app.editor.cursor_col), (2, 4));
assert!(app.status.contains("read-only"), "{}", app.status);
use std::os::unix::fs::PermissionsExt;
assert_eq!(
std::fs::metadata(&opened).unwrap().permissions().mode() & 0o222,
0
);
app.editor.open_preview(&log).unwrap();
select(&mut app, "R2");
app.open_selected_sarif_result();
let opened = app.editor.path.clone().expect("a copy opened");
assert_eq!(std::fs::read(&opened).unwrap(), [0, 1, 2, 255]);
assert!(
app.editor.has_non_text_view(),
"binary opens in the hex viewer"
);
});
}
#[test]
fn a_sarif_log_rewritten_on_disk_keeps_the_readers_place() {
let tmp = tempfile::tempdir().unwrap();
let result = |rule: &str, line: u32| {
format!(
r#"{{"ruleId":"{rule}","level":"warning","message":{{"text":"{rule} here"}},"locations":[{{"physicalLocation":{{"artifactLocation":{{"uri":"src/{rule}.rs"}},"region":{{"startLine":{line}}}}}}}]}}"#
)
};
let log_of = |results: &[String]| {
format!(
r#"{{"version":"2.1.0","runs":[{{"tool":{{"driver":{{"name":"lint"}}}},"results":[{}]}}]}}"#,
results.join(",")
)
};
let main = tmp.path().join("main.sarif");
let extra = tmp.path().join("extra.sarif");
std::fs::write(&main, log_of(&[result("Aa", 1), result("Bb", 2)])).unwrap();
std::fs::write(&extra, log_of(&[result("Cc", 3)])).unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&main).unwrap();
{
let view = app.editor.sarif.as_mut().unwrap();
let log = crate::sarif::load::parse_log(&std::fs::read_to_string(&extra).unwrap()).unwrap();
assert!(view.add_log(&extra, log));
view.query_text = String::from("-Cc");
view.filters
.hidden_levels
.insert(crate::sarif::semantics::Level::Note);
view.collapsed.insert(String::from("some-group"));
let rows = view.rows();
view.selected = rows
.iter()
.position(|r| matches!(r, crate::sarif::view::Row::Item { entry } if view.entries[*entry].rule_id == "Bb"))
.unwrap();
}
std::fs::write(
&main,
log_of(&[result("Zz", 9), result("Aa", 1), result("Bb", 2)]),
)
.unwrap();
app.editor.revert_to_disk().unwrap();
let view = app.editor.sarif.as_ref().unwrap();
assert_eq!(view.logs.len(), 2, "the added log is still there");
assert_eq!(view.query_text, "-Cc");
assert!(
view.filters
.hidden_levels
.contains(&crate::sarif::semantics::Level::Note)
);
assert!(view.collapsed.contains("some-group"));
assert_eq!(view.entries.len(), 4, "the rewritten log was read");
assert_eq!(
view.selected_entry().map(|e| e.rule_id.as_str()),
Some("Bb"),
"the same result stays selected though it moved"
);
}
#[test]
fn sarif_columns_are_chosen_with_c_shown_on_rows_and_remembered() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.minimap.visible = false;
assert!(
app.editor.sarif.as_ref().unwrap().columns.is_empty(),
"none by default"
);
let draw = |app: &mut App| {
let mut term =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(160, 30)).unwrap();
term.draw(|f| app.render(f)).unwrap();
screen_text(&term)
};
let row = |screen: &str| {
screen
.lines()
.find(|l| l.contains("Query built from user input."))
.and_then(|l| {
let end = l.find("input.")? + "input.".len();
Some(l[..end].to_string())
})
.unwrap_or_default()
};
assert!(
!row(&draw(&mut app)).contains("R1 "),
"{}",
row(&draw(&mut app))
);
app.handle_sarif_key(key(KeyCode::Char('C'), KeyModifiers::NONE));
app.input_prompt.as_mut().unwrap().value = String::from("rule, colour");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(
app.status.contains("No column \"colour\""),
"{}",
app.status
);
assert!(app.status.contains("rule, level, kind"), "{}", app.status);
app.handle_sarif_key(key(KeyCode::Char('C'), KeyModifiers::NONE));
app.input_prompt.as_mut().unwrap().value = String::from("rule, baseline");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.status, "SARIF columns: rule, baseline");
let shown = row(&draw(&mut app));
let (rule, base, msg) = (
shown.find("R1").expect(&shown),
shown.find("no baseline").expect(&shown),
shown.find("Query built").unwrap(),
);
assert!(rule < base && base < msg, "in the chosen order: {shown}");
let mut again = App::new(tmp.path().to_path_buf()).unwrap();
again.editor.open(&log).unwrap();
assert_eq!(
again.editor.sarif.as_ref().unwrap().columns,
[
crate::sarif::view::ExtraColumn::Rule,
crate::sarif::view::ExtraColumn::Baseline
]
);
again.handle_sarif_key(key(KeyCode::Char('C'), KeyModifiers::NONE));
assert_eq!(again.input_prompt.as_ref().unwrap().value, "rule,baseline");
});
}
#[test]
fn sarif_filter_words_are_highlighted_in_the_results() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.editor.sarif.as_mut().unwrap().set_query("user");
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(140, 30)).unwrap();
term.draw(|f| app.render(f)).unwrap();
let buf = term.backend().buffer().clone();
let (y, line) = (0..buf.area.height)
.map(|y| {
let s: String = (0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect();
(y, s)
})
.find(|(_, s)| s.contains("Query built from user input."))
.expect("the row is drawn");
let at = line[..line.find("user input").unwrap()].chars().count() as u16;
let marked = |x: u16| {
buf[(x, y)]
.modifier
.contains(ratatui::style::Modifier::UNDERLINED)
};
assert!((at..at + 4).all(marked), "\"user\" is marked");
assert!(!marked(at + 4) && !marked(at - 2), "and nothing around it");
});
}
#[test]
fn a_sarif_results_steps_are_drawn_in_their_own_files_only() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
std::fs::write(
tmp.path().join("src/a.rs"),
"fn a() {\n let x = input();\n sink(x);\n}\n",
)
.unwrap();
std::fs::write(
tmp.path().join("src/b.rs"),
"fn input() -> String {\n read()\n}\n",
)
.unwrap();
let step = |uri: &str, line: u32, msg: &str| {
format!(
r#"{{"location":{{"message":{{"text":"{msg}"}},"physicalLocation":{{"artifactLocation":{{"uri":"{uri}"}},"region":{{"startLine":{line}}}}}}}}}"#
)
};
let log = tmp.path().join("flow.sarif");
std::fs::write(
&log,
format!(
r#"{{"version":"2.1.0","runs":[{{"tool":{{"driver":{{"name":"t"}}}},"results":[{{"ruleId":"R1","message":{{"text":"tainted"}},
"locations":[{{"physicalLocation":{{"artifactLocation":{{"uri":"src/a.rs"}},"region":{{"startLine":3}}}}}}],
"codeFlows":[{{"threadFlows":[{{"locations":[{},{},{}]}}]}}]}}]}}]}}"#,
step("src/b.rs", 2, "source"),
step("src/a.rs", 2, "assigned"),
step("src/a.rs", 3, "sink")
),
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
app.open_selected_sarif_result();
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("src/a.rs").as_path())
);
app.sync_sarif_step_marks();
let labels = |app: &App, line: usize| -> Vec<String> {
app.editor
.inlay_spans_for_test(line)
.iter()
.map(|(_, l, _)| l.trim().to_string())
.collect()
};
assert_eq!(labels(&app, 1), ["\u{25c2} step 2: assigned"]);
assert_eq!(labels(&app, 2), ["\u{25c2} step 3: sink"]);
assert!(
labels(&app, 0).is_empty(),
"b.rs's step is not drawn in a.rs"
);
let (col, _, _) = &app.editor.inlay_spans_for_test(1)[0];
assert_eq!(*col, " let x = input();".chars().count());
}
#[test]
fn sarif_filter_typing_narrows_the_list() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.handle_sarif_key(key(KeyCode::Char('/'), KeyModifiers::NONE));
for c in "nomatch".chars() {
app.handle_sarif_key(key(KeyCode::Char(c), KeyModifiers::NONE));
}
let view = app.editor.sarif.as_ref().unwrap();
assert_eq!(view.query_text, "nomatch");
assert!(view.rows().is_empty());
app.handle_sarif_key(key(KeyCode::Esc, KeyModifiers::NONE));
let view = app.editor.sarif.as_ref().unwrap();
assert_eq!(view.query_text, "");
assert_eq!(view.rows().len(), 2);
}
#[test]
fn sarif_that_does_not_load_opens_as_text_and_says_why() {
let tmp = tempfile::tempdir().unwrap();
let bad = tmp.path().join("bad.sarif");
std::fs::write(&bad, "{\n \"version\": \"2.1.0\",\n oops\n}\n").unwrap();
let old = tmp.path().join("old.sarif");
std::fs::write(&old, r#"{"version":"1.0.0","runs":[]}"#).unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&bad).unwrap();
assert!(app.editor.sarif.is_none());
assert_eq!(app.editor.lines[2], " oops");
assert!(
app.editor.status.contains("invalid JSON at 3:"),
"status names the position: {}",
app.editor.status
);
app.editor.open(&old).unwrap();
assert!(app.editor.sarif.is_none());
assert!(
app.editor.status.contains("1.0.0"),
"status names the version: {}",
app.editor.status
);
}
#[cfg(unix)]
#[test]
fn a_codeql_query_suite_runs_through_database_analyze_into_sarif() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
use crate::widgets::command_palette::Command;
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let mut app = codeql_query_fixture(tmp.path(), "select 1");
app.codeql_jobs.program =
fake_codeql(bin.path(), r#"{"version":"2.1.0","runs":[]}"#, 0, "");
let alert = tmp.path().join("Alert.ql");
std::fs::write(&alert, "/** @kind problem */ select 1").unwrap();
let listed = bin.path().join("resolved.json");
std::fs::write(&listed, format!("[\"{}\"]", alert.display())).unwrap();
let script = std::fs::read_to_string(&app.codeql_jobs.program).unwrap();
std::fs::write(
&app.codeql_jobs.program,
script.replacen(
"\n",
&format!(
"\n[ \"$1\" = resolve ] && {{ cat '{}'; exit 0; }}\n",
listed.display()
),
1,
),
)
.unwrap();
app.run_command(Command::CodeqlRunQuerySuite);
assert_eq!(app.status, "Open a .qls query suite to run it");
assert!(app.codeql_jobs.run.is_none());
let suite = tmp.path().join("s.qls");
std::fs::write(&suite, "- queries: .\n").unwrap();
app.editor.open(&suite).unwrap();
app.editor.dirty = true;
app.run_command(Command::CodeqlRunQuerySuite);
assert_eq!(
app.status,
"Save s.qls before running it: CodeQL reads it from disk"
);
assert!(app.codeql_jobs.run.is_none());
app.editor.dirty = false;
app.run_command(Command::CodeqlRunQuerySuite);
assert!(
app.status.contains("Running s.qls on app"),
"{}",
app.status
);
wait_for_codeql(&mut app);
let opened = app.editor.path.clone().expect("the results open");
assert_eq!(opened.extension().and_then(|e| e.to_str()), Some("sarif"));
let run = &app.codeql.history[0];
assert_eq!(
(&run.status, run.name.as_str(), run.database.as_str()),
(&crate::codeql_query::RunStatus::Succeeded, "s.qls", "app"),
"{:?}",
app.codeql.history
);
let calls = std::fs::read_to_string(bin.path().join("codeql-calls.log")).unwrap();
assert!(
calls.contains(&format!(
"database analyze {} {} --format=sarif-latest ",
tmp.path().join("dbs/app").display(),
suite.display()
)),
"{calls}"
);
let table = tmp.path().join("Table.ql");
std::fs::write(&table, "select 1").unwrap();
std::fs::write(
&listed,
format!("[\"{}\",\"{}\"]", alert.display(), table.display()),
)
.unwrap();
app.editor.open(&suite).unwrap();
app.run_command(Command::CodeqlRunQuerySuite);
wait_for_codeql(&mut app);
assert!(
matches!(
&app.codeql.history[0].status,
crate::codeql_query::RunStatus::Failed(why) if why.contains("Table.ql")
),
"the refusal names the table query: {:?}",
app.codeql.history
);
app.editor.open(&suite).unwrap();
app.run_command(Command::CodeqlRunQuery);
assert!(
app.status.contains("Running s.qls on app"),
"{}",
app.status
);
wait_for_codeql(&mut app);
assert_eq!(
Command::from_id("codeql_run_query_suite"),
Some(Command::CodeqlRunQuerySuite)
);
});
}
fn sarif_flow_fixture() -> (tempfile::TempDir, std::path::PathBuf) {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
std::fs::write(
tmp.path().join("src/api.rs"),
"fn a() {}\nlet input = read();\n",
)
.unwrap();
std::fs::write(
tmp.path().join("src/db.rs"),
"one\ntwo\nthree\nrun(input);\n",
)
.unwrap();
let log = tmp.path().join("flow.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"lint","rules":[{"id":"R1","name":"Taint",
"help":{"text":"Use parameters instead."},"properties":{"tags":["security"]}}]}},
"results":[{"ruleId":"R1","level":"error",
"message":{"text":"Tainted by [the input](1)."},
"locations":[{"physicalLocation":{"artifactLocation":{"uri":"src/db.rs"},"region":{"startLine":4,"startColumn":1}}}],
"relatedLocations":[{"id":1,"physicalLocation":{"artifactLocation":{"uri":"src/api.rs"},"region":{"startLine":2,"startColumn":5}}}],
"codeFlows":[{"threadFlows":[{"locations":[
{"location":{"message":{"text":"source"},"physicalLocation":{"artifactLocation":{"uri":"src/api.rs"},"region":{"startLine":2,"startColumn":5}}}},
{"location":{"message":{"text":"sink"},"physicalLocation":{"artifactLocation":{"uri":"src/db.rs"},"region":{"startLine":4,"startColumn":5}}}}]}]}],
"stacks":[{"frames":[{"location":{"message":{"text":"frame zero"},"physicalLocation":{"artifactLocation":{"uri":"src/db.rs"},"region":{"startLine":3,"startColumn":2}}}}]}]
}]}]}"#,
)
.unwrap();
(tmp, log)
}
#[test]
fn sarif_info_tab_shows_rule_help_tags_and_related_locations() {
let (tmp, log) = sarif_flow_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
let screen = draw_screen(&mut app, 160, 40);
assert!(
screen.contains("Use parameters instead."),
"rule help:\n{screen}"
);
assert!(screen.contains("security"), "tags:\n{screen}");
assert!(
screen.contains("src/api.rs:2:5"),
"related location:\n{screen}"
);
}
#[test]
fn sarif_steps_tab_walks_the_code_flow_in_the_editor() {
let (tmp, log) = sarif_flow_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
app.handle_sarif_key(key(KeyCode::Char('d'), KeyModifiers::NONE));
let screen = draw_screen(&mut app, 160, 40);
assert!(screen.contains("Steps"), "{screen}");
assert!(
screen.contains("source") && screen.contains("sink"),
"{screen}"
);
app.handle_sarif_key(key(KeyCode::Char('n'), KeyModifiers::NONE));
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("src/api.rs").as_path()),
"step 1 opened: {}",
app.status
);
assert_eq!((app.editor.cursor_row, app.editor.cursor_col), (1, 4));
let viewer = (0..app.editor.tab_count())
.find(|&i| app.editor.tab_path(i).as_deref() == Some(log.as_path()))
.expect("the viewer tab stayed open");
app.editor.select(viewer);
app.handle_sarif_key(key(KeyCode::Char('n'), KeyModifiers::NONE));
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("src/db.rs").as_path())
);
assert_eq!((app.editor.cursor_row, app.editor.cursor_col), (3, 4));
}
#[test]
fn sarif_stacks_and_raw_tabs() {
let (tmp, log) = sarif_flow_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.handle_sarif_key(key(KeyCode::Char('d'), KeyModifiers::NONE));
app.handle_sarif_key(key(KeyCode::Char('d'), KeyModifiers::NONE));
let screen = draw_screen(&mut app, 160, 40);
assert!(screen.contains("frame zero"), "stacks tab:\n{screen}");
assert!(
screen.contains("src/db.rs:3:2"),
"frame location with its column:\n{screen}"
);
app.handle_sarif_key(key(KeyCode::Char('d'), KeyModifiers::NONE));
let screen = draw_screen(&mut app, 160, 40);
assert!(screen.contains("\"ruleId\": \"R1\""), "raw tab:\n{screen}");
app.handle_sarif_key(key(KeyCode::Char('D'), KeyModifiers::NONE));
let screen = draw_screen(&mut app, 160, 40);
assert!(
screen.contains("frame zero"),
"D goes back a tab:\n{screen}"
);
}
#[test]
fn sarif_message_link_opens_its_related_location() {
let (tmp, log) = sarif_flow_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
app.handle_sarif_key(key(KeyCode::Char('L'), KeyModifiers::NONE));
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("src/api.rs").as_path()),
"[the input](1) followed: {}",
app.status
);
assert_eq!((app.editor.cursor_row, app.editor.cursor_col), (1, 4));
}
#[test]
fn an_open_sarif_log_publishes_its_results_as_diagnostics() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
assert!(app.sync_sarif_diagnostics(), "publishing is a change");
let target = tmp.path().join("src/a.rs");
let by_source = app
.lsp
.diagnostics
.get(&target)
.expect("the result's file has diagnostics");
let diags = by_source.get("SARIF: lint").expect("keyed by the tool");
assert_eq!(diags.len(), 1);
let d = &diags[0];
assert_eq!((d.start_line, d.start_char), (1, 8));
assert_eq!(d.end_line, 1);
assert_eq!(
d.end_char as usize,
" let q = format!(\"{}\", id);".encode_utf16().count(),
"no endColumn: the range runs to the end of the start line"
);
assert_eq!(d.severity, crate::lsp::manager::DiagnosticSeverity::Error);
assert_eq!(d.message, "[R1] Query built from user input.");
assert!(
!app.sync_sarif_diagnostics(),
"an unchanged log is not a change"
);
app.editor.pin_active();
app.editor.open_preview(&target).unwrap();
app.sync_sarif_diagnostics();
let merged = app.merged_diagnostics(&target);
assert!(merged.iter().any(|d| d.message.contains("[R1]")));
}
#[test]
fn closing_the_sarif_tab_withdraws_its_diagnostics() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.sync_sarif_diagnostics();
let target = tmp.path().join("src/a.rs");
assert!(app.lsp.diagnostics.contains_key(&target));
app.editor.open(&target).unwrap();
assert!(app.sync_sarif_diagnostics(), "withdrawing is a change");
assert!(
app.lsp
.diagnostics
.get(&target)
.is_none_or(|m| !m.contains_key("SARIF: lint")),
"nothing left behind"
);
}
#[test]
fn sarif_levels_map_to_editor_severities() {
use crate::lsp::manager::DiagnosticSeverity as S;
use crate::sarif::semantics::Level;
assert_eq!(crate::sarif::diagnostics::severity(Level::Error), S::Error);
assert_eq!(
crate::sarif::diagnostics::severity(Level::Warning),
S::Warning
);
assert_eq!(
crate::sarif::diagnostics::severity(Level::Note),
S::Information
);
assert_eq!(
crate::sarif::diagnostics::severity(Level::None),
S::Information
);
}
fn sarif_fix_fixture() -> (tempfile::TempDir, std::path::PathBuf) {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("q.rs"), "let q = format!(\"{}\", id);\n").unwrap();
let log = tmp.path().join("fix.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"lint"}},"columnKind":"unicodeCodePoints",
"results":[{"ruleId":"R9","level":"warning","message":{"text":"Unsanitised id."},
"locations":[{"physicalLocation":{"artifactLocation":{"uri":"q.rs"},"region":{"startLine":1,"startColumn":23}}}],
"fixes":[{"description":{"text":"Sanitise it"},"artifactChanges":[{"artifactLocation":{"uri":"q.rs"},
"replacements":[{"deletedRegion":{"startLine":1,"startColumn":23,"endColumn":25},"insertedContent":{"text":"clean(id)"}}]}]}]}]}]}"#,
)
.unwrap();
(tmp, log)
}
#[test]
fn sarif_fix_tab_previews_and_f_applies_the_fix_as_an_unsaved_edit() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let (tmp, log) = sarif_fix_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&log).unwrap();
for _ in 0..4 {
app.handle_sarif_key(key(KeyCode::Char('d'), KeyModifiers::NONE));
}
let screen = draw_screen(&mut app, 160, 40);
assert!(
screen.contains("Sanitise it"),
"the fix's description:\n{screen}"
);
assert!(
screen.contains("+ let q = format!(\"{}\", clean(id));"),
"its preview:\n{screen}"
);
app.handle_sarif_key(key(KeyCode::Char('f'), KeyModifiers::NONE));
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("q.rs").as_path()),
"the fixed file is open: {}",
app.status
);
assert_eq!(app.editor.lines[0], "let q = format!(\"{}\", clean(id));");
assert!(
app.editor.dirty,
"applied to the buffer, not saved behind the user's back"
);
assert_eq!(
std::fs::read_to_string(tmp.path().join("q.rs")).unwrap(),
"let q = format!(\"{}\", id);\n",
"the disk is untouched"
);
let viewer = (0..app.editor.tab_count())
.find(|&i| app.editor.tab_path(i).as_deref() == Some(log.as_path()))
.unwrap();
app.editor.select(viewer);
assert!(app.editor.sarif.as_ref().unwrap().entries[0].fixed);
let mut again = App::new(tmp.path().to_path_buf()).unwrap();
again.editor.open(&log).unwrap();
assert!(
again.editor.sarif.as_ref().unwrap().entries[0].fixed,
"persisted"
);
again.sync_sarif_diagnostics();
assert!(
again
.lsp
.diagnostics
.get(&tmp.path().join("q.rs"))
.is_none_or(|m| m.values().all(|v| v.is_empty())),
"fixed results are withdrawn from the editor"
);
});
}
fn sarif_two_fixes_fixture() -> (tempfile::TempDir, std::path::PathBuf) {
let tmp = tempfile::tempdir().unwrap();
let file = tmp.path().join("util.py");
std::fs::write(
&file,
"import os, sys\ndef long_function_name(x):\n if x == None:\n",
)
.unwrap();
let log = tmp.path().join("ruff.sarif");
let result = |rule: &str, line: u32, (from, to): (u32, u32), text: &str| {
format!(
r#"{{"ruleId":"{rule}","level":"error","message":{{"text":"{rule}"}},
"locations":[{{"physicalLocation":{{"artifactLocation":{{"uri":"util.py"}},"region":{{"startLine":{line},"startColumn":{from}}}}}}}],
"fixes":[{{"description":{{"text":"fix {rule}"}},"artifactChanges":[{{"artifactLocation":{{"uri":"util.py"}},
"replacements":[{{"deletedRegion":{{"startLine":{line},"startColumn":{from},"endColumn":{to}}},"insertedContent":{{"text":"{text}"}}}}]}}]}}]}}"#
)
};
std::fs::write(
&log,
format!(
r#"{{"version":"2.1.0","runs":[{{"tool":{{"driver":{{"name":"ruff"}}}},"columnKind":"unicodeCodePoints","results":[{},{}]}}]}}"#,
result("E401", 1, (1, 15), "import os\\nimport sys"),
result("E711", 3, (8, 17), "x is None"),
),
)
.unwrap();
for (path, secs) in [(&file, 1_000), (&log, 2_000)] {
std::fs::File::options()
.write(true)
.open(path)
.unwrap()
.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs))
.unwrap();
}
(tmp, log)
}
#[test]
fn a_sarif_fix_for_a_file_changed_since_the_log_is_refused() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let (tmp, log) = sarif_two_fixes_fixture();
let util = tmp.path().join("util.py");
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
let fix = |app: &mut App, entry: usize| {
let viewer = (0..app.editor.tab_count())
.find(|&i| app.editor.tab_path(i).as_deref() == Some(log.as_path()))
.unwrap();
app.editor.select(viewer);
let view = app.editor.sarif.as_mut().unwrap();
view.selected = view
.rows()
.iter()
.position(|r| *r == crate::sarif::view::Row::Item { entry })
.unwrap();
app.handle_sarif_key(key(KeyCode::Char('f'), KeyModifiers::NONE));
};
fix(&mut app, 0);
assert_eq!(app.editor.path.as_deref(), Some(util.as_path()));
assert_eq!(
app.editor.lines[..3],
["import os", "import sys", "def long_function_name(x):"]
);
fix(&mut app, 1);
assert_eq!(
app.status,
"Fix not applied: util.py changed since the analysis; re-run the analyzer"
);
assert!(!app.editor.sarif.as_ref().unwrap().entries[1].fixed);
let util_tab = (0..app.editor.tab_count())
.find(|&i| app.editor.tab_path(i).as_deref() == Some(util.as_path()))
.unwrap();
app.editor.select(util_tab);
assert_eq!(app.editor.lines[2], "def long_function_name(x):");
app.save();
assert!(!app.editor.dirty, "{}", app.status);
fix(&mut app, 1);
assert!(
app.status.contains("util.py changed since the analysis"),
"{}",
app.status
);
assert_eq!(
std::fs::read_to_string(&util).unwrap(),
"import os\nimport sys\ndef long_function_name(x):\n if x == None:\n"
);
});
}
#[test]
fn next_and_previous_sarif_result_walk_locations_across_files() {
let (tmp, log) = sarif_nav_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.editor.pin_active();
app.open_at(&tmp.path().join("a.rs"), 0, 0).unwrap();
use crate::widgets::command_palette::Command;
app.run_command(Command::SarifNextResult);
assert_eq!(
(app.editor.cursor_row, app.status.contains("first")),
(1, true),
"{}",
app.status
);
app.run_command(Command::SarifNextResult);
assert_eq!(app.editor.cursor_row, 4);
app.run_command(Command::SarifNextResult);
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("b.rs").as_path()),
"on to the next file"
);
app.run_command(Command::SarifNextResult);
assert_eq!(
(app.editor.path.as_deref(), app.editor.cursor_row),
(Some(tmp.path().join("a.rs").as_path()), 1),
"wraps to the first"
);
app.run_command(Command::SarifPreviousResult);
assert_eq!(
app.editor.path.as_deref(),
Some(tmp.path().join("b.rs").as_path())
);
assert_eq!(Command::SarifNextResult.title(), "SARIF: Next Result");
}
#[test]
fn a_sarif_log_from_no_codeql_run_still_asks_to_locate_its_file() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let app_dir = tmp.path().join("cq/app");
std::fs::create_dir_all(&app_dir).unwrap();
std::fs::write(app_dir.join("web.py"), WEB_PY_1369).unwrap();
let db = tmp.path().join("db");
std::fs::create_dir_all(&db).unwrap();
std::fs::write(
db.join(crate::codeql_db::DB_MARKER),
format!("sourceLocationPrefix: {}\n", app_dir.display()),
)
.unwrap();
let log = codeql_run_on(tmp.path(), &db);
let other = tmp.path().join("ci.sarif");
std::fs::copy(&log, &other).unwrap();
let mut app = App::new(tmp.path().join("cq")).unwrap();
app.editor.open_preview(&other).unwrap();
app.editor.sarif.as_mut().unwrap().selected = 1;
app.open_selected_sarif_result();
assert!(
matches!(
app.input_prompt.as_ref().map(|p| &p.purpose),
Some(crate::widgets::input_prompt::InputPurpose::SarifLocate { .. })
),
"a Locate prompt: {}",
app.status
);
});
}
#[test]
fn the_run_tab_shows_the_tool_its_invocation_and_notifications() {
let tmp = tempfile::tempdir().unwrap();
let log = tmp.path().join("run.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{
"tool":{"driver":{"name":"CodeQL","semanticVersion":"2.19.3","informationUri":"https://codeql.github.com"}},
"invocations":[{"commandLine":"codeql database analyze db --format=sarif-latest","executionSuccessful":true,"exitCode":0,
"startTimeUtc":"2026-09-24T10:00:00Z","endTimeUtc":"2026-09-24T10:03:12Z",
"toolExecutionNotifications":[{"level":"warning","message":{"text":"Skipped vendor/huge.js: too large"}}]}],
"results":[]}]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
for _ in 0..3 {
app.handle_sarif_key(key(KeyCode::Tab, KeyModifiers::NONE));
}
assert_eq!(
app.editor.sarif.as_ref().unwrap().tab,
crate::sarif::view::Tab::Run
);
let screen = draw_screen(&mut app, 160, 40);
for want in [
"CodeQL 2.19.3",
"https://codeql.github.com",
"codeql database analyze db --format=sarif-latest",
"exit 0",
"Skipped vendor/huge.js: too large",
] {
assert!(screen.contains(want), "{want:?} on the Run tab:\n{screen}");
}
app.handle_sarif_key(key(KeyCode::Tab, KeyModifiers::NONE));
assert_eq!(
app.editor.sarif.as_ref().unwrap().tab,
crate::sarif::view::Tab::Locations
);
}
#[test]
fn e_exports_the_visible_sarif_results_to_a_csv_file() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.handle_sarif_key(key(KeyCode::Char('E'), KeyModifiers::SHIFT));
let prompt = app.input_prompt.as_ref().expect("E asks where to write");
assert!(matches!(
prompt.purpose,
crate::widgets::input_prompt::InputPurpose::SarifExport
));
assert_eq!(
prompt.value, "sarif-results.csv",
"a default name is offered"
);
app.submit_sarif_export("out/results.csv");
let written = std::fs::read_to_string(tmp.path().join("out/results.csv"))
.expect("written relative to the workspace, creating the folder");
assert!(
written.starts_with("rule,level,file,line,column,message"),
"{written}"
);
assert_eq!(
written.lines().count(),
2,
"one result plus the header: {written}"
);
assert!(app.status.contains("1 result"), "{}", app.status);
}
#[test]
fn the_sarif_key_legend_names_the_fix_log_baseline_and_export_keys() {
let (tmp, log) = sarif_fixture();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
let screen = draw_screen(&mut app, 160, 40);
for want in ["f fix", "o add log", "b baseline", "E export"] {
assert!(screen.contains(want), "{want} missing:\n{screen}");
}
}
#[cfg(unix)]
fn fake_gh(dir: &std::path::Path, rules: &[(&str, &str)]) -> std::path::PathBuf {
use std::os::unix::fs::PermissionsExt;
let mut script = String::from(
"#!/bin/sh\necho \"$*\" >> \"$(dirname \"$0\")/gh-calls.log\"\ncase \"$*\" in\n",
);
for (i, (pattern, body)) in rules.iter().enumerate() {
let file = dir.join(format!("gh-reply-{i}"));
std::fs::write(&file, body).unwrap();
script.push_str(&format!(" *'{pattern}'*) cat '{}' ;;\n", file.display()));
}
script.push_str(" *) echo '{\"message\":\"unexpected call\"}' >&2; exit 1 ;;\nesac\n");
let gh = dir.join("gh");
std::fs::write(&gh, script).unwrap();
std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap();
gh
}
fn settle_code_scanning(app: &mut App) {
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"code scanning's gh calls",
|| {
app.poll_code_scanning();
app.code_scan_asks.is_empty()
},
);
}
#[cfg(unix)]
#[test]
fn a_code_scanning_analysis_opens_from_github_in_the_sarif_viewer() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let gh = fake_gh(
bin.path(),
&[
(
"analyses?per_page=30",
r#"[{"id":201,"ref":"refs/heads/main","commit_sha":"abc","error":"","category":"",
"created_at":"2026-09-20T10:00:00Z","results_count":1,"tool":{"name":"CodeQL","version":"2.19.3"}}]"#,
),
(
"analyses/201",
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"CodeQL"}},
"results":[{"ruleId":"rust/sql-injection","message":{"text":"From GitHub."}}]}]}"#,
),
],
);
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = gh;
app.run_command(crate::widgets::command_palette::Command::SarifOpenCodeScanning);
settle_code_scanning(&mut app);
let picker = app.list_picker.as_ref().expect("the analyses are offered");
assert_eq!(
picker.purpose,
crate::widgets::list_picker::ListPurpose::CodeScanningAnalysis
);
assert_eq!(picker.rows.len(), 1);
assert_eq!(picker.rows[0].id, "201");
assert!(
picker.rows[0].label.contains("CodeQL 2.19.3"),
"{}",
picker.rows[0].label
);
app.confirm_list_picker();
settle_code_scanning(&mut app);
let view = app
.editor
.sarif
.as_ref()
.expect("the analysis opens in the viewer");
assert!(view.entries.iter().any(|e| e.message == "From GitHub."));
let calls = std::fs::read_to_string(bin.path().join("gh-calls.log")).unwrap();
assert!(calls.contains("Accept: application/sarif+json"), "{calls}");
});
}
#[cfg(unix)]
#[test]
fn code_scanning_reports_githubs_own_error() {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let gh = fake_gh(bin.path(), &[]);
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = gh;
app.run_command(crate::widgets::command_palette::Command::SarifOpenCodeScanning);
settle_code_scanning(&mut app);
assert!(app.list_picker.is_none());
assert!(app.status.contains("unexpected call"), "{}", app.status);
}
#[cfg(unix)]
#[test]
fn x_dismisses_the_selected_results_code_scanning_alert_with_a_reason() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join("src")).unwrap();
std::fs::write(tmp.path().join("src/db.rs"), "a\nb\nc\n").unwrap();
let log = tmp.path().join("scan.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"CodeQL"}},"results":[
{"ruleId":"rust/sql-injection","message":{"text":"Tainted."},
"locations":[{"physicalLocation":{"artifactLocation":{"uri":"src/db.rs"},"region":{"startLine":2}}}]}]}]}"#,
)
.unwrap();
let bin = tempfile::tempdir().unwrap();
let gh = fake_gh(
bin.path(),
&[
(
"code-scanning/alerts?per_page=100",
r#"[{"number":8,"state":"open","rule":{"id":"rust/sql-injection"},
"most_recent_instance":{"location":{"path":"src/db.rs","start_line":2}}}]"#,
),
("PATCH", r#"{"number":8,"state":"dismissed"}"#),
],
);
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = gh;
app.editor.open(&log).unwrap();
app.editor.sarif.as_mut().unwrap().selected = 1;
app.handle_sarif_key(key(KeyCode::Char('X'), KeyModifiers::SHIFT));
settle_code_scanning(&mut app);
let picker = app.list_picker.as_ref().expect("X asks why");
assert_eq!(
picker.purpose,
crate::widgets::list_picker::ListPurpose::DismissAlert
);
let labels: Vec<&str> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["False positive", "Won't fix", "Used in tests"]);
app.list_picker.as_mut().unwrap().selected = 1;
app.confirm_list_picker();
assert!(matches!(
app.input_prompt.as_ref().map(|p| &p.purpose),
Some(
crate::widgets::input_prompt::InputPurpose::DismissAlertComment {
number: 8,
reason: 1
}
)
));
app.dismiss_code_scanning_alert(8, 1, "tracked elsewhere");
settle_code_scanning(&mut app);
let calls = std::fs::read_to_string(bin.path().join("gh-calls.log")).unwrap();
assert!(
calls.contains("PATCH repos/{owner}/{repo}/code-scanning/alerts/8 -f state=dismissed -f dismissed_reason=won't fix -f dismissed_comment=tracked elsewhere"),
"{calls}"
);
assert!(app.status.contains("#8"), "{}", app.status);
let view = app.editor.sarif.as_ref().unwrap();
assert_eq!(
view.entries[0].suppression,
crate::sarif::semantics::SuppressionState::Suppressed,
"the dismissed result leaves the default list"
);
}
#[test]
fn the_info_tab_lists_the_results_taxa() {
let tmp = tempfile::tempdir().unwrap();
let log = tmp.path().join("t.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"T"}},
"taxonomies":[{"name":"CWE","taxa":[{"id":"CWE-89","name":"SqlInjection"}]}],
"results":[{"ruleId":"R1","message":{"text":"m"},
"taxa":[{"id":"CWE-89","toolComponent":{"name":"CWE"}}]}]}]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.editor.sarif.as_mut().unwrap().selected = 1;
let screen = draw_screen(&mut app, 160, 40);
assert!(screen.contains("CWE CWE-89: SqlInjection"), "{screen}");
}
#[cfg(unix)]
#[test]
fn code_scanning_offers_the_current_branchs_analyses_and_falls_back_to_all() {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let one = |id: u64, git_ref: &str| {
format!(
r#"[{{"id":{id},"ref":"{git_ref}","commit_sha":"c","error":"","category":"","created_at":"2026-09-20T10:00:00Z","results_count":0,"tool":{{"name":"CodeQL"}}}}]"#
)
};
let on_branch = one(301, "refs/heads/feat/x");
let everything = one(300, "refs/heads/main");
let gh = fake_gh(
bin.path(),
&[
("ref=refs%2Fheads%2Ffeat%2Fx", &on_branch),
("ref=refs%2Fheads%2Fempty", "[]"),
("analyses?per_page=30", &everything),
],
);
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = gh;
app.source_control.status.branch = Some(String::from("feat/x"));
app.run_command(crate::widgets::command_palette::Command::SarifOpenCodeScanning);
settle_code_scanning(&mut app);
let picker = app.list_picker.take().expect("offered");
assert_eq!(
picker
.rows
.iter()
.map(|r| r.id.as_str())
.collect::<Vec<_>>(),
["301"]
);
assert!(picker.title.contains("feat/x"), "{}", picker.title);
app.source_control.status.branch = Some(String::from("empty"));
app.run_command(crate::widgets::command_palette::Command::SarifOpenCodeScanning);
settle_code_scanning(&mut app);
let picker = app.list_picker.take().expect("offered");
assert_eq!(
picker
.rows
.iter()
.map(|r| r.id.as_str())
.collect::<Vec<_>>(),
["300"]
);
}
#[test]
fn a_sarif_source_past_its_cap_is_refused_not_cut_short() {
use std::io::{Read as _, Write as _};
const CAP: usize = 64 << 20;
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
std::thread::spawn(move || {
for (stream, len) in listener.incoming().zip([CAP + 1, CAP]) {
let Ok(mut s) = stream else { break };
let mut buf = [0u8; 4096];
let _ = s.read(&mut buf);
let head =
format!("HTTP/1.1 200 OK\r\ncontent-length: {len}\r\nconnection: close\r\n\r\n");
let _ = s.write_all(head.as_bytes());
let _ = s.write_all(&vec![b'a'; len]);
}
});
let url = format!("http://127.0.0.1:{port}/big.rs");
let refused = match fetch_sarif_source(&url) {
Ok(bytes) => panic!("a file past the cap came back as {} bytes", bytes.len()),
Err(e) => e,
};
assert!(
refused.contains("larger than its 64 MiB limit"),
"{refused}"
);
assert_eq!(fetch_sarif_source(&url).map(|b| b.len()), Ok(CAP));
}
#[cfg(unix)]
#[test]
fn a_dismissal_goes_to_the_repository_it_began_in() {
use std::os::unix::fs::PermissionsExt;
let a = tempfile::tempdir().unwrap();
let b = tempfile::tempdir().unwrap();
std::fs::create_dir_all(a.path().join("src")).unwrap();
std::fs::write(a.path().join("src/db.rs"), "a\nb\nc\n").unwrap();
std::fs::write(b.path().join("other.rs"), "x\n").unwrap();
let log = a.path().join("scan.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"CodeQL"}},"results":[
{"ruleId":"rust/sql-injection","message":{"text":"Tainted."},
"locations":[{"physicalLocation":{"artifactLocation":{"uri":"src/db.rs"},"region":{"startLine":2}}}]}]}]}"#,
)
.unwrap();
let bin = tempfile::tempdir().unwrap();
let calls = bin.path().join("gh-calls.log");
let gh = bin.path().join("gh");
std::fs::write(
&gh,
format!(
"#!/bin/sh\necho \"$(pwd) $*\" >> '{}'\ncase \"$*\" in\n *alerts?per_page*) echo '[{{\"number\":8,\"state\":\"open\",\"rule\":{{\"id\":\"rust/sql-injection\"}},\"most_recent_instance\":{{\"location\":{{\"path\":\"src/db.rs\",\"start_line\":2}}}}}}]' ;;\n *) echo '{{}}' ;;\nesac\n",
calls.display()
),
)
.unwrap();
std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap();
let mut app = App::new(a.path().to_path_buf()).unwrap();
app.add_workspace_folder(b.path().to_path_buf());
app.gh_program = gh;
app.editor.open(&log).unwrap();
app.editor.sarif.as_mut().unwrap().selected = 1;
app.handle_sarif_key(key(KeyCode::Char('X'), KeyModifiers::SHIFT));
app.editor.open(&b.path().join("other.rs")).unwrap();
assert_eq!(app.active_workspace_root(), b.path());
settle_code_scanning(&mut app);
assert!(app.list_picker.is_some(), "X asks why");
app.list_picker = None;
app.dismiss_code_scanning_alert(8, 1, "tracked elsewhere");
settle_code_scanning(&mut app);
let calls = std::fs::read_to_string(&calls).unwrap();
let patch = calls
.lines()
.find(|l| l.contains("PATCH"))
.unwrap_or_else(|| panic!("no dismissal was sent: {calls}"));
assert!(
patch.starts_with(&format!("{} ", a.path().display())),
"the dismissal ran in {patch}"
);
}
#[cfg(unix)]
fn stalled_gh(dir: &std::path::Path) -> std::path::PathBuf {
use std::os::unix::fs::PermissionsExt;
let gh = dir.join("gh");
std::fs::write(&gh, "#!/bin/sh\nexec sleep 30\n").unwrap();
std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap();
gh
}
#[cfg(unix)]
#[test]
fn opening_a_code_scanning_analysis_never_waits_on_gh() {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = stalled_gh(bin.path());
app.gh_limit = std::time::Duration::from_secs(4);
let started = std::time::Instant::now();
app.run_command(crate::widgets::command_palette::Command::SarifOpenCodeScanning);
assert!(
started.elapsed() < std::time::Duration::from_secs(2),
"the command waited {:?} on gh",
started.elapsed()
);
assert_eq!(app.status, "Fetching code scanning analyses…");
settle_code_scanning(&mut app);
assert!(app.list_picker.is_none());
assert!(
app.status.ends_with("took longer than 4s"),
"{}",
app.status
);
}
#[cfg(unix)]
#[test]
fn a_stalled_code_scanning_lookup_ends_at_the_limit() {
let tmp = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = stalled_gh(bin.path());
app.gh_limit = std::time::Duration::from_secs(1);
app.source_control.status.in_repo = true;
app.source_control.status.branch = Some(String::from("main"));
app.code_scanning = crate::sarif::github::CodeScanningMode::On;
app.poll_code_scanning();
assert!(app.code_scan_list.is_some(), "the branch is looked up");
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"the stalled lookup to end",
|| {
app.poll_code_scanning();
app.code_scan_list.is_none()
},
);
assert!(
app.status.starts_with("Code scanning: ") && app.status.ends_with("took longer than 1s"),
"{}",
app.status
);
}
#[test]
fn sarif_export_writes_the_visible_results_as_sarif_or_csv() {
use crate::sarif::semantics::Level;
let tmp = tempfile::tempdir().unwrap();
let log = tmp.path().join("two.sarif");
std::fs::write(
&log,
r#"{"version":"2.1.0","runs":[
{"tool":{"driver":{"name":"lint","rules":[{"id":"R1"},{"id":"R2"}]}},
"results":[{"ruleId":"R1","level":"error","message":{"text":"bad"}},
{"ruleId":"R2","level":"note","message":{"text":"meh"}}]},
{"tool":{"driver":{"name":"other"}},
"results":[{"ruleId":"X","level":"note","message":{"text":"quiet"}}]}]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&log).unwrap();
app.editor
.sarif
.as_mut()
.unwrap()
.filters
.hidden_levels
.insert(Level::Note);
app.submit_sarif_export("out.sarif");
assert!(app.status.contains("Exported 1 result"), "{}", app.status);
let out: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(tmp.path().join("out.sarif")).unwrap())
.unwrap();
assert_eq!(out["version"], "2.1.0");
let runs = out["runs"].as_array().unwrap();
assert_eq!(runs.len(), 1, "the run left empty is dropped");
assert_eq!(runs[0]["tool"]["driver"]["name"], "lint");
assert_eq!(
runs[0]["tool"]["driver"]["rules"].as_array().unwrap().len(),
2
);
let results = runs[0]["results"].as_array().unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0]["ruleId"], "R1");
app.editor.open(&tmp.path().join("out.sarif")).unwrap();
assert_eq!(app.editor.sarif.as_ref().unwrap().entries.len(), 1);
app.editor.open(&log).unwrap();
app.submit_sarif_export("out.csv");
let csv = std::fs::read_to_string(tmp.path().join("out.csv")).unwrap();
assert!(csv.starts_with("rule,level,file"), "{csv}");
}
#[test]
fn code_scanning_follows_the_branch_when_on_and_only_says_so_when_prompting() {
use std::os::unix::fs::PermissionsExt;
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let git = |args: &[&str]| git_ok(tmp.path(), args);
git_init_with_identity(tmp.path());
std::fs::write(tmp.path().join("a.txt"), "a").unwrap();
git(&["add", "."]);
git(&["commit", "-q", "-m", "scanned"]);
let scanned = git(&["rev-parse", "HEAD"]);
std::fs::write(tmp.path().join("a.txt"), "b").unwrap();
git(&["commit", "-q", "-am", "not scanned yet"]);
let log = tmp.path().join("gh-args");
let gh = tmp.path().join("gh");
std::fs::write(
&gh,
format!(
r#"#!/bin/sh
echo "$@" >> '{log}'
case "$*" in
*sarif+json*) echo '{{"version": "2.1.0", "runs": []}}' ;;
*) echo '[{{"id": 7, "ref": "refs/heads/main", "commit_sha": "{scanned}", "tool": {{"name": "CodeQL"}}, "created_at": "2026-09-01T00:00:00Z", "results_count": 0}}, {{"id": 6, "ref": "refs/heads/main", "commit_sha": "0000", "tool": {{"name": "CodeQL"}}, "created_at": "2026-09-02T00:00:00Z", "results_count": 0}}]' ;;
esac
"#,
log = log.display()
),
)
.unwrap();
std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.gh_program = gh.clone();
app.source_control.status.in_repo = true;
app.source_control.status.branch = Some(String::from("main"));
app.poll_code_scanning();
assert!(
app.code_scan_list.is_none() && !log.exists(),
"off never calls gh"
);
app.code_scanning = crate::sarif::github::CodeScanningMode::On;
let settle = |app: &mut App| {
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"code scanning",
|| {
app.poll_code_scanning();
app.code_scan_list.is_none() && app.code_scan_fetch.is_none()
},
);
};
app.poll_code_scanning();
settle(&mut app);
assert_eq!(app.status, "Loaded 1 code scanning analysis");
assert!(
app.editor.sarif.is_some(),
"the analysis opened in the viewer"
);
let calls = std::fs::read_to_string(&log).unwrap();
assert!(
calls.contains("ref=refs%2Fheads%2Fmain") || calls.contains("ref=refs/heads/main"),
"{calls}"
);
assert!(
calls.contains("analyses/7"),
"the one at the nearest scanned commit: {calls}"
);
assert!(!calls.contains("analyses/6"), "{calls}");
std::fs::remove_file(&log).unwrap();
app.poll_code_scanning();
assert!(app.code_scan_list.is_none() && !log.exists());
app.code_scanning = crate::sarif::github::CodeScanningMode::Prompt;
app.source_control.status.branch = None;
app.source_control.status.detached_hash = Some(scanned.clone());
app.poll_code_scanning();
settle(&mut app);
assert!(
app.status
.starts_with("Code scanning has 1 analysis for detached HEAD"),
"{}",
app.status
);
let calls = std::fs::read_to_string(&log).unwrap();
assert!(
!calls.contains("sarif+json"),
"prompt downloads nothing: {calls}"
);
});
}