#![allow(dead_code)]
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
widgets::Widget,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Section {
Language,
Databases,
Queries,
VariantAnalysis,
QueryHistory,
AstViewer,
EvaluatorLog,
MethodModeling,
}
impl Section {
pub const ALL: [Section; 8] = [
Section::Language,
Section::Databases,
Section::Queries,
Section::VariantAnalysis,
Section::QueryHistory,
Section::AstViewer,
Section::EvaluatorLog,
Section::MethodModeling,
];
pub fn title(self) -> &'static str {
match self {
Section::Language => "LANGUAGE",
Section::Databases => "DATABASES",
Section::Queries => "QUERIES",
Section::VariantAnalysis => "VARIANT ANALYSIS REPOSITORIES",
Section::QueryHistory => "QUERY HISTORY",
Section::AstViewer => "AST VIEWER",
Section::EvaluatorLog => "EVALUATOR LOG VIEWER",
Section::MethodModeling => "METHOD MODELING",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
AddDatabaseFromFolder,
AddDatabaseFromArchive,
AddDatabaseFromUrl,
AddDatabaseFromGithub,
CreateQuery,
SetUpControllerRepository,
ViewAst,
AstNode(usize),
ClearAst,
EvalPredicate(usize),
EvalDependency(Option<usize>),
ClearEvalLog,
OpenModelEditor,
ModelGroup(usize),
ModelEndpoint(usize),
SelectLanguage(usize),
SelectDatabase(usize),
SortDatabases,
SortHistory,
OpenHistory(usize),
TogglePack(usize),
RunQuery(usize, usize),
VariantList(usize),
VariantRepo(Option<usize>, usize),
VariantOwner(usize),
AddVariantRepo,
AddVariantList,
AddVariantOwner,
OpenVariantConfig,
VariantRun(usize),
}
pub const LANGUAGES: [&str; 10] = [
"C / C++",
"C#",
"GitHub Actions",
"Go",
"Java / Kotlin",
"JavaScript / TypeScript",
"Python",
"Ruby",
"Rust",
"Swift",
];
pub const LANGUAGE_IDS: [&str; 10] = [
"cpp",
"csharp",
"actions",
"go",
"java",
"javascript",
"python",
"ruby",
"rust",
"swift",
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Line {
Header(Section),
Text(&'static str),
Action(Action, String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Hit {
Header(Section),
Action(Action),
}
#[derive(Debug, Default)]
pub struct CodeqlPanel {
pub collapsed: std::collections::HashSet<Section>,
pub selected: usize,
pub scroll: usize,
pub language: Option<usize>,
pub focused: bool,
pub theme: crate::theme::Theme,
pub last_area: Rect,
pub databases: Vec<crate::codeql_db::DbEntry>,
pub current_db: Option<usize>,
pub db_sort: Option<crate::codeql_db::DbSort>,
pub history: Vec<String>,
pub history_sort: crate::codeql_query::HistSort,
pub queries: Vec<crate::codeql_query::QueryPack>,
pub folded_packs: std::collections::HashSet<std::path::PathBuf>,
pub variant: crate::codeql_variant::VariantConfig,
pub variant_error: bool,
pub folded_lists: std::collections::HashSet<String>,
pub variant_runs: Vec<String>,
pub ast: Option<crate::codeql_ast::AstView>,
pub evallog: Option<crate::codeql_evallog::LogView>,
pub model: Option<crate::codeql_model::ModelView>,
}
impl CodeqlPanel {
pub fn new() -> Self {
Self {
collapsed: std::collections::HashSet::from([Section::Language]),
..Self::default()
}
}
pub fn pack_matches_language(&self, pack: &crate::codeql_query::QueryPack) -> bool {
match (self.language, pack.language.as_deref()) {
(Some(i), Some(lang)) => LANGUAGE_IDS[i] == lang,
_ => true,
}
}
pub fn db_matches_language(&self, db: &crate::codeql_db::DbEntry) -> bool {
match (self.language, db.language.as_deref()) {
(Some(i), Some(lang)) => crate::codeql_db::language_label(lang) == Some(LANGUAGES[i]),
_ => true,
}
}
pub fn selected_database(&self) -> Option<usize> {
match self.selected_hit() {
Some(Hit::Action(Action::SelectDatabase(i))) => Some(i),
_ => None,
}
}
pub fn select_database(&mut self, index: usize) {
if let Some(n) = self
.lines()
.iter()
.position(|l| matches!(l, Line::Action(Action::SelectDatabase(i), _) if *i == index))
{
self.selected = n;
}
}
pub fn selected_model_endpoint(&self) -> Option<usize> {
match self.selected_hit() {
Some(Hit::Action(Action::ModelEndpoint(i))) => Some(i),
_ => None,
}
}
pub fn select_evallog_predicate(&mut self, i: usize) {
let row = self.lines().iter().position(|l| {
matches!(l, Line::Action(Action::EvalPredicate(p), t) if *p == i && !t.starts_with(' '))
});
if let Some(row) = row {
self.selected = row;
}
}
pub fn selected_history(&self) -> Option<usize> {
match self.selected_hit() {
Some(Hit::Action(Action::OpenHistory(i))) => Some(i),
_ => None,
}
}
pub fn select_history(&mut self, index: usize) {
if let Some(n) = self
.lines()
.iter()
.position(|l| matches!(l, Line::Action(Action::OpenHistory(i), _) if *i == index))
{
self.selected = n;
}
}
pub fn toggle_pack(&mut self, pack: usize) {
let Some(dir) = self.queries.get(pack).map(|p| p.dir.clone()) else {
return;
};
if !self.folded_packs.remove(&dir) {
self.folded_packs.insert(dir);
}
if let Some(n) = self
.lines()
.iter()
.position(|l| matches!(l, Line::Action(Action::TogglePack(p), _) if *p == pack))
{
self.selected = n;
}
}
pub fn selected_pack(&self) -> Option<usize> {
match self.selected_hit() {
Some(Hit::Action(Action::TogglePack(p) | Action::RunQuery(p, _))) => Some(p),
_ => None,
}
}
pub fn selected_variant_item(&self) -> Option<crate::codeql_variant::Item> {
use crate::codeql_variant::Item;
match self.selected_hit() {
Some(Hit::Action(Action::VariantList(i))) => Some(Item::List(i)),
Some(Hit::Action(Action::VariantRepo(l, j))) => Some(Item::Repo(l, j)),
Some(Hit::Action(Action::VariantOwner(i))) => Some(Item::Owner(i)),
_ => None,
}
}
pub fn selected_variant_run(&self) -> Option<usize> {
match self.selected_hit() {
Some(Hit::Action(Action::VariantRun(i))) => Some(i),
_ => None,
}
}
pub fn selected_variant_list(&self) -> Option<usize> {
use crate::codeql_variant::Item;
match self.selected_variant_item() {
Some(Item::List(i) | Item::Repo(Some(i), _)) => Some(i),
_ => None,
}
}
pub fn select_variant_item(&mut self, item: crate::codeql_variant::Item) {
use crate::codeql_variant::Item;
let action = match item {
Item::List(i) => Action::VariantList(i),
Item::Repo(l, j) => Action::VariantRepo(l, j),
Item::Owner(i) => Action::VariantOwner(i),
};
self.select_action(action);
}
pub fn select_action(&mut self, action: Action) {
if let Some(n) = self
.lines()
.iter()
.position(|l| matches!(l, Line::Action(a, _) if *a == action))
{
self.selected = n;
}
}
pub fn toggle_variant_list(&mut self, list: usize) {
let Some(name) = self.variant.lists.get(list).map(|l| l.name.clone()) else {
return;
};
if !self.folded_lists.remove(&name) {
self.folded_lists.insert(name);
}
self.select_variant_item(crate::codeql_variant::Item::List(list));
}
pub fn selected_section(&self) -> Option<Section> {
let lines = self.lines();
let end = self.selected.min(lines.len().saturating_sub(1));
lines[..=end].iter().rev().find_map(|l| match l {
Line::Header(s) => Some(*s),
_ => None,
})
}
fn variant_lines(&self, out: &mut Vec<Line>) {
use crate::codeql_variant::Item;
if self.variant_error {
out.push(Line::Text("The variant analysis config file could"));
out.push(Line::Text("not be read. Fix or remove it to go on."));
out.push(Line::Action(
Action::OpenVariantConfig,
"Open config file".to_string(),
));
return;
}
let v = &self.variant;
match &v.controller_repo {
None => {
out.push(Line::Text("Set up a controller repository to start"));
out.push(Line::Text("using variant analysis."));
out.push(Line::Action(
Action::SetUpControllerRepository,
"Set up controller repository".to_string(),
));
}
Some(c) => out.push(Line::Action(
Action::SetUpControllerRepository,
format!("Controller: {c}"),
)),
}
let mark = |item| if v.is_selected(item) { "●" } else { "○" };
for (i, list) in v.lists.iter().enumerate() {
let folded = self.folded_lists.contains(&list.name);
let chevron = if folded {
crate::icons::CHEVRON_CLOSED
} else {
crate::icons::CHEVRON_OPEN
};
out.push(Line::Action(
Action::VariantList(i),
format!(
"{chevron} {} {} ({})",
mark(Item::List(i)),
list.name,
list.repos.len()
),
));
if folded {
continue;
}
for (j, nwo) in list.repos.iter().enumerate() {
out.push(Line::Action(
Action::VariantRepo(Some(i), j),
format!(" {} {nwo}", mark(Item::Repo(Some(i), j))),
));
}
}
for (j, nwo) in v.repos.iter().enumerate() {
out.push(Line::Action(
Action::VariantRepo(None, j),
format!("{} {nwo}", mark(Item::Repo(None, j))),
));
}
for (i, owner) in v.owners.iter().enumerate() {
out.push(Line::Action(
Action::VariantOwner(i),
format!("{} {owner} (owner)", mark(Item::Owner(i))),
));
}
if v.controller_repo.is_some() {
for (a, label) in [
(Action::AddVariantRepo, "Add repository"),
(Action::AddVariantList, "Add repository list"),
(Action::AddVariantOwner, "Add owner"),
] {
out.push(Line::Action(a, label.to_string()));
}
}
if !self.variant_runs.is_empty() {
out.push(Line::Text("Runs (newest first):"));
for (i, label) in self.variant_runs.iter().enumerate().rev() {
out.push(Line::Action(Action::VariantRun(i), label.clone()));
}
}
}
pub fn lines(&self) -> Vec<Line> {
let mut out = Vec::new();
for s in Section::ALL {
out.push(Line::Header(s));
if self.collapsed.contains(&s) {
continue;
}
match s {
Section::Language => {
for (i, name) in LANGUAGES.iter().enumerate() {
let mark = if self.language == Some(i) {
"●"
} else {
"○"
};
out.push(Line::Action(
Action::SelectLanguage(i),
format!("{mark} {name}"),
));
}
}
Section::Databases => {
if !self.databases.is_empty() {
let by = self.db_sort.map_or("date added", |b| b.label());
out.push(Line::Action(
Action::SortDatabases,
format!("Sort by: {by}"),
));
}
let mut shown = 0;
for (i, db) in self.databases.iter().enumerate() {
if !self.db_matches_language(db) {
continue;
}
shown += 1;
let mark = if self.current_db == Some(i) {
"●"
} else {
"○"
};
let lang = db
.language
.as_deref()
.map(|l| format!(" ({l})"))
.unwrap_or_default();
out.push(Line::Action(
Action::SelectDatabase(i),
format!("{mark} {}{lang}", db.name),
));
}
if shown == 0 && !self.databases.is_empty() {
out.push(Line::Text("No databases in this language."));
}
out.push(Line::Text("Add a CodeQL database:"));
for (a, label) in [
(Action::AddDatabaseFromFolder, "From a folder"),
(Action::AddDatabaseFromArchive, "From an archive"),
(Action::AddDatabaseFromUrl, "From a URL (as a zip file)"),
(Action::AddDatabaseFromGithub, "From GitHub"),
] {
out.push(Line::Action(a, label.to_string()));
}
}
Section::Queries if self.queries.iter().any(|p| self.pack_matches_language(p)) => {
for (i, pack) in self.queries.iter().enumerate() {
if !self.pack_matches_language(pack) {
continue;
}
let folded = self.folded_packs.contains(&pack.dir);
let chevron = if folded {
crate::icons::CHEVRON_CLOSED
} else {
crate::icons::CHEVRON_OPEN
};
let lang = pack
.language
.as_deref()
.map(|l| format!(" ({l})"))
.unwrap_or_default();
out.push(Line::Action(
Action::TogglePack(i),
format!("{chevron} {}{lang}", pack.name),
));
if folded {
continue;
}
for (j, q) in pack.queries.iter().enumerate() {
let rel = q.strip_prefix(&pack.dir).unwrap_or(q);
out.push(Line::Action(
Action::RunQuery(i, j),
format!(" {}", rel.display()),
));
}
}
}
Section::Queries => {
out.push(Line::Text("We didn't find any CodeQL queries in"));
out.push(Line::Text("this workspace."));
out.push(Line::Action(
Action::CreateQuery,
"Create one to get started".to_string(),
));
}
Section::VariantAnalysis => self.variant_lines(&mut out),
Section::QueryHistory if !self.history.is_empty() => {
out.push(Line::Action(
Action::SortHistory,
format!("Sort by: {}", self.history_sort.label()),
));
for (i, label) in self.history.iter().enumerate() {
out.push(Line::Action(Action::OpenHistory(i), label.clone()));
}
}
Section::QueryHistory => {
out.push(Line::Text("You have no query history items at the"));
out.push(Line::Text("moment. Select a database to run a CodeQL"));
out.push(Line::Text("query and get your first results."));
}
Section::AstViewer => match &self.ast {
Some(view) => {
let name = view
.file
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
out.push(Line::Action(
Action::ClearAst,
format!("Clear \u{b7} {name} in {}", view.database),
));
for (depth, i) in view.visible() {
let node = &view.tree.nodes[i];
let mark = match (node.children.is_empty(), view.open.contains(&i)) {
(true, _) => ' ',
(false, true) => '\u{25be}',
(false, false) => '\u{25b8}',
};
let at = node
.location
.as_ref()
.map(|l| format!(" {}:{}", l.line, l.column))
.unwrap_or_default();
out.push(Line::Action(
Action::AstNode(i),
format!("{}{mark} {}{at}", " ".repeat(depth), node.label),
));
}
}
None => {
out.push(Line::Text("Run 'CodeQL: View AST' on an open source"));
out.push(Line::Text("file from a CodeQL database."));
out.push(Line::Action(Action::ViewAst, "View AST".to_string()));
}
},
Section::EvaluatorLog => match &self.evallog {
Some(view) => {
out.push(Line::Action(
Action::ClearEvalLog,
format!("Clear \u{b7} {}", view.query),
));
use crate::codeql_evallog::LogRow;
for row in view.rows() {
out.push(match row {
LogRow::Predicate(i, text) | LogRow::Detail(i, text) => {
Line::Action(Action::EvalPredicate(i), text)
}
LogRow::Dependency(_, target, text) => {
Line::Action(Action::EvalDependency(target), text)
}
});
}
}
None => {
out.push(Line::Text("Run 'Show Evaluator Log (Viewer)' on a"));
out.push(Line::Text("query history item."));
}
},
Section::MethodModeling => match &self.model {
Some(view) => {
out.push(Line::Action(
Action::OpenModelEditor,
format!("Refresh \u{b7} {} ({})", view.database, view.language),
));
use crate::codeql_model::ModelRow;
let rows = view.rows();
for (at, row) in rows.iter().enumerate() {
out.push(match row {
ModelRow::Group(group, modeled, total) => {
let mark = if view.folded.contains(group) {
'\u{25b8}'
} else {
'\u{25be}'
};
let first = rows[at..].iter().find_map(|r| match r {
ModelRow::Endpoint(i, _) => Some(*i),
_ => None,
});
let first = first.unwrap_or_else(|| {
view.endpoints
.iter()
.position(|e| e.group() == *group)
.unwrap_or(0)
});
Line::Action(
Action::ModelGroup(first),
format!("{mark} {group} {modeled}/{total} modeled"),
)
}
ModelRow::Endpoint(i, text) => {
Line::Action(Action::ModelEndpoint(*i), text.clone())
}
});
}
}
None => {
out.push(Line::Text("Model the library methods of the"));
out.push(Line::Text("current database as sources, sinks"));
out.push(Line::Text("or summaries."));
out.push(Line::Action(
Action::OpenModelEditor,
"Open Model Editor".to_string(),
));
}
},
}
}
out
}
fn selectable(line: &Line) -> Option<Hit> {
match line {
Line::Header(s) => Some(Hit::Header(*s)),
Line::Action(a, _) => Some(Hit::Action(*a)),
Line::Text(_) => None,
}
}
pub fn move_selection(&mut self, down: bool) {
let lines = self.lines();
let mut i = self.selected.min(lines.len().saturating_sub(1));
loop {
let next = if down {
i.checked_add(1)
} else {
i.checked_sub(1)
};
match next {
Some(n) if n < lines.len() => {
i = n;
if Self::selectable(&lines[n]).is_some() {
self.selected = n;
return;
}
}
_ => return,
}
}
}
pub fn selected_hit(&self) -> Option<Hit> {
self.lines().get(self.selected).and_then(Self::selectable)
}
pub fn toggle(&mut self, section: Section) {
if !self.collapsed.remove(§ion) {
self.collapsed.insert(section);
}
if let Some(n) = self
.lines()
.iter()
.position(|l| *l == Line::Header(section))
{
self.selected = n;
}
}
pub fn hit_at(&self, _x: u16, y: u16) -> Option<(usize, Hit)> {
let a = self.last_area;
if a.height == 0 || y <= a.y || y >= a.y + a.height {
return None;
}
let idx = self.scroll + (y - a.y - 1) as usize;
let lines = self.lines();
lines.get(idx).and_then(Self::selectable).map(|h| (idx, h))
}
pub fn scroll_down(&mut self, n: usize) {
let max = self.lines().len().saturating_sub(1);
self.scroll = (self.scroll + n).min(max);
}
pub fn scroll_up(&mut self, n: usize) {
self.scroll = self.scroll.saturating_sub(n);
}
}
impl Widget for &mut CodeqlPanel {
fn render(self, area: Rect, buf: &mut Buffer) {
self.last_area = area;
if area.height < 2 || area.width < 8 {
return;
}
let theme = self.theme;
let fg = theme.ui(Color::Rgb(0xcc, 0xcc, 0xcc));
let dim = Color::Rgb(0x8b, 0x94, 0x9e);
let link = theme.accent();
let w = area.width as usize;
buf.set_stringn(
area.x + 1,
area.y,
"CODEQL",
w.saturating_sub(1),
Style::default().fg(fg).add_modifier(Modifier::BOLD),
);
let lines = self.lines();
let rows = (area.height - 1) as usize;
if self.selected < self.scroll {
self.scroll = self.selected;
} else if self.selected >= self.scroll + rows {
self.scroll = self.selected + 1 - rows;
}
for (r, line) in lines.iter().enumerate().skip(self.scroll).take(rows) {
let y = area.y + 1 + (r - self.scroll) as u16;
let selected = self.focused && r == self.selected;
let base = if selected {
Style::default()
.fg(theme.accent_contrast_fg())
.bg(theme.accent())
} else {
Style::default()
};
let (text, style) = match line {
Line::Header(s) => {
let chevron = if self.collapsed.contains(s) {
crate::icons::CHEVRON_CLOSED
} else {
crate::icons::CHEVRON_OPEN
};
let title = match s {
Section::Language => format!(
"{} · {}",
s.title(),
self.language.map_or("All", |i| LANGUAGES[i])
),
_ => s.title().to_string(),
};
(
format!("{chevron} {title}"),
base.fg(if selected { base.fg.unwrap_or(fg) } else { fg })
.add_modifier(Modifier::BOLD),
)
}
Line::Text(t) => (format!(" {t}"), base.fg(dim)),
Line::Action(_, label) => (
format!(" {label}"),
if selected {
base
} else {
base.fg(link).add_modifier(Modifier::UNDERLINED)
},
),
};
buf.set_stringn(area.x, y, &text, w, style);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_section_is_listed_with_its_welcome() {
let p = CodeqlPanel::new();
assert!(p.collapsed.contains(&Section::Language), "starts folded");
let lines = p.lines();
for s in Section::ALL {
assert!(lines.contains(&Line::Header(s)), "{s:?}");
}
assert!(lines.contains(&Line::Action(
Action::AddDatabaseFromGithub,
"From GitHub".into()
)));
}
#[test]
fn selection_skips_text_and_folding_keeps_it_on_the_header() {
let mut p = CodeqlPanel::new();
assert_eq!(p.selected_hit(), Some(Hit::Header(Section::Language)));
p.toggle(Section::Language);
p.move_selection(true);
assert_eq!(
p.selected_hit(),
Some(Hit::Action(Action::SelectLanguage(0)))
);
p.toggle(Section::Language);
assert_eq!(p.selected_hit(), Some(Hit::Header(Section::Language)));
p.move_selection(true);
assert_eq!(p.selected_hit(), Some(Hit::Header(Section::Databases)));
p.move_selection(true);
assert_eq!(
p.selected_hit(),
Some(Hit::Action(Action::AddDatabaseFromFolder)),
"the 'Add a CodeQL database:' text line is skipped"
);
p.move_selection(false);
p.move_selection(false);
p.move_selection(false);
assert_eq!(
p.selected_hit(),
Some(Hit::Header(Section::Language)),
"clamps at the top"
);
}
#[test]
fn databases_list_with_the_current_one_marked() {
let mut p = CodeqlPanel::new();
p.databases = vec![
crate::codeql_db::DbEntry {
name: "a-db".into(),
path: "/x/a-db".into(),
language: Some("python".into()),
added: 0,
former_names: Vec::new(),
},
crate::codeql_db::DbEntry {
name: "b-db".into(),
path: "/x/b-db".into(),
language: Some("go".into()),
added: 0,
former_names: Vec::new(),
},
];
p.current_db = Some(1);
let lines = p.lines();
assert!(lines.contains(&Line::Action(
Action::SelectDatabase(0),
"○ a-db (python)".into()
)));
assert!(lines.contains(&Line::Action(
Action::SelectDatabase(1),
"● b-db (go)".into()
)));
assert!(
lines.contains(&Line::Action(
Action::AddDatabaseFromGithub,
"From GitHub".into()
)),
"adding more stays on offer"
);
}
#[test]
fn databases_follow_the_language_and_keep_their_store_index() {
let db = |name: &str, lang: Option<&str>| crate::codeql_db::DbEntry {
name: name.into(),
path: format!("/x/{name}").into(),
language: lang.map(Into::into),
added: 0,
former_names: Vec::new(),
};
let mut p = CodeqlPanel::new();
p.databases = vec![
db("gin", Some("go")),
db("web", Some("typescript")),
db("odd", None),
];
assert!(
p.lines().contains(&Line::Action(
Action::SortDatabases,
"Sort by: date added".into()
)),
"the list offers its order"
);
p.db_sort = Some(crate::codeql_db::DbSort::Name);
assert!(
p.lines()
.contains(&Line::Action(Action::SortDatabases, "Sort by: name".into()))
);
p.language = Some(5);
let dbs: Vec<Action> = p
.lines()
.into_iter()
.filter_map(|l| match l {
Line::Action(a @ Action::SelectDatabase(_), _) => Some(a),
_ => None,
})
.collect();
assert_eq!(
dbs,
[Action::SelectDatabase(1), Action::SelectDatabase(2)],
"store indices, and the unknown language shows under any"
);
p.select_database(1);
assert_eq!(p.selected_database(), Some(1));
p.databases.pop();
p.language = Some(9);
assert!(
p.lines()
.contains(&Line::Text("No databases in this language."))
);
}
#[test]
fn language_selection_marks_one() {
let mut p = CodeqlPanel::new();
p.toggle(Section::Language);
p.language = Some(6);
assert!(
p.lines()
.contains(&Line::Action(Action::SelectLanguage(6), "● Python".into()))
);
}
#[test]
fn query_history_lists_each_run_to_open_and_keeps_its_welcome_when_empty() {
let mut p = CodeqlPanel::new();
assert!(
p.lines()
.contains(&Line::Text("You have no query history items at the"))
);
p.history = vec![
String::from("\u{2713} a.ql \u{b7} app \u{b7} 3s"),
String::from("\u{2717} b.ql \u{b7} app \u{b7} failed: x"),
];
let lines = p.lines();
assert!(!lines.contains(&Line::Text("You have no query history items at the")));
assert!(lines.contains(&Line::Action(
Action::OpenHistory(0),
"\u{2713} a.ql \u{b7} app \u{b7} 3s".into()
)));
assert!(lines.contains(&Line::Action(
Action::OpenHistory(1),
"\u{2717} b.ql \u{b7} app \u{b7} failed: x".into()
)));
assert!(
lines.contains(&Line::Action(Action::SortHistory, "Sort by: date".into())),
"the list offers its order"
);
p.history_sort = crate::codeql_query::HistSort::Name;
assert!(
p.lines()
.contains(&Line::Action(Action::SortHistory, "Sort by: name".into()))
);
p.select_history(1);
assert_eq!(p.selected_history(), Some(1));
}
fn pack(
name: &str,
language: Option<&str>,
dir: &str,
queries: &[&str],
) -> crate::codeql_query::QueryPack {
crate::codeql_query::QueryPack {
name: name.into(),
language: language.map(Into::into),
dir: dir.into(),
queries: queries
.iter()
.map(|q| format!("{dir}/{q}").into())
.collect(),
}
}
#[test]
fn queries_list_by_pack_and_follow_the_language() {
let mut p = CodeqlPanel::new();
assert!(
p.lines()
.contains(&Line::Text("We didn't find any CodeQL queries in"))
);
p.queries = vec![
pack("acme/go", Some("go"), "/w/go", &["a.ql", "sub/b.ql"]),
pack("acme/py", Some("python"), "/w/py", &["c.ql"]),
];
let lines = p.lines();
assert!(!lines.contains(&Line::Text("We didn't find any CodeQL queries in")));
let open = crate::icons::CHEVRON_OPEN;
assert!(lines.contains(&Line::Action(
Action::TogglePack(0),
format!("{open} acme/go (go)")
)));
assert!(lines.contains(&Line::Action(Action::RunQuery(0, 1), " sub/b.ql".into())));
assert!(lines.contains(&Line::Action(Action::RunQuery(1, 0), " c.ql".into())));
p.language = Some(6);
let lines = p.lines();
assert!(
!lines
.iter()
.any(|l| matches!(l, Line::Action(Action::RunQuery(0, _), _)))
);
assert!(lines.contains(&Line::Action(Action::RunQuery(1, 0), " c.ql".into())));
p.language = Some(9);
assert!(
p.lines()
.contains(&Line::Text("We didn't find any CodeQL queries in"))
);
p.queries
.push(pack(crate::codeql_query::NO_PACK, None, "/w", &["x.ql"]));
assert!(
p.lines()
.contains(&Line::Action(Action::RunQuery(2, 0), " x.ql".into()))
);
}
#[test]
fn a_pack_folds_and_keeps_the_selection_on_its_line() {
let mut p = CodeqlPanel::new();
p.queries = vec![pack("acme/go", Some("go"), "/w/go", &["a.ql"])];
p.toggle_pack(0);
assert_eq!(p.selected_hit(), Some(Hit::Action(Action::TogglePack(0))));
assert!(
!p.lines()
.iter()
.any(|l| matches!(l, Line::Action(Action::RunQuery(..), _)))
);
p.toggle_pack(0);
assert_eq!(p.selected_pack(), Some(0));
p.move_selection(true);
assert_eq!(p.selected_hit(), Some(Hit::Action(Action::RunQuery(0, 0))));
assert_eq!(p.selected_pack(), Some(0), "a query row is in its pack");
p.toggle(Section::Queries);
assert_eq!(p.selected_pack(), None);
}
#[test]
fn variant_analysis_lists_the_controller_lists_repos_and_owners() {
use crate::codeql_variant::Item;
let mut p = CodeqlPanel::new();
p.variant.lists.push(crate::codeql_variant::RepoList {
name: "top".into(),
repos: vec!["a/b".into(), "c/d".into()],
});
p.variant.repos.push("e/f".into());
p.variant.owners.push("octo".into());
let lines = p.lines();
assert!(lines.contains(&Line::Action(
Action::SetUpControllerRepository,
"Set up controller repository".into()
)));
assert!(!lines.contains(&Line::Action(
Action::AddVariantRepo,
"Add repository".into()
)));
p.variant.controller_repo = Some("me/ctl".into());
p.variant.select(Item::Repo(Some(0), 1)).unwrap();
let open = crate::icons::CHEVRON_OPEN;
let va: Vec<Line> = p
.lines()
.into_iter()
.skip_while(|l| *l != Line::Header(Section::VariantAnalysis))
.skip(1)
.take_while(|l| !matches!(l, Line::Header(_)))
.collect();
assert_eq!(
va,
[
Line::Action(
Action::SetUpControllerRepository,
"Controller: me/ctl".into()
),
Line::Action(Action::VariantList(0), format!("{open} ○ top (2)")),
Line::Action(Action::VariantRepo(Some(0), 0), " ○ a/b".into()),
Line::Action(Action::VariantRepo(Some(0), 1), " ● c/d".into()),
Line::Action(Action::VariantRepo(None, 0), "○ e/f".into()),
Line::Action(Action::VariantOwner(0), "○ octo (owner)".into()),
Line::Action(Action::AddVariantRepo, "Add repository".into()),
Line::Action(Action::AddVariantList, "Add repository list".into()),
Line::Action(Action::AddVariantOwner, "Add owner".into()),
]
);
p.select_variant_item(Item::Repo(Some(0), 1));
assert_eq!(p.selected_variant_list(), Some(0));
assert_eq!(p.selected_section(), Some(Section::VariantAnalysis));
p.toggle_variant_list(0);
assert_eq!(p.selected_variant_item(), Some(Item::List(0)));
assert!(
!p.lines()
.iter()
.any(|l| matches!(l, Line::Action(Action::VariantRepo(Some(_), _), _)))
);
p.select_variant_item(Item::Owner(0));
assert_eq!(p.selected_variant_list(), None);
p.variant_error = true;
let lines = p.lines();
assert!(lines.contains(&Line::Action(
Action::OpenVariantConfig,
"Open config file".into()
)));
assert!(
!lines
.iter()
.any(|l| matches!(l, Line::Action(Action::VariantOwner(_), _)))
);
}
}