Skip to content

Commit

Permalink
Clean up prepended psql error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel committed Nov 9, 2023
1 parent 64ef467 commit a913e15
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
44 changes: 28 additions & 16 deletions cli/src/commands/regress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs::File,
io::{BufWriter, Cursor, Read, Write},
ops::Not,
path::{Path, PathBuf},
};

Expand All @@ -18,7 +19,7 @@ use super::SubCommand;
type Result<T = ()> = std::result::Result<T, anyhow::Error>;

#[derive(Args)]
pub struct TestCommand {
pub struct RegressCommand {
/// The name of the extension to test
extension_name: String,
/// The psql connection string on which commands will be executed in
Expand All @@ -27,12 +28,7 @@ pub struct TestCommand {

#[derive(Deserialize, Debug)]
struct TrunkProjectInfo {
pub name: String,
pub description: String,
pub documentation_link: String,
pub repository_link: String,
pub version: String,
pub extensions: Vec<Extension>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -125,7 +121,7 @@ async fn extract_sql_and_expected_files(
}

#[async_trait::async_trait]
impl SubCommand for TestCommand {
impl SubCommand for RegressCommand {
async fn execute(&self, _: Task) -> Result<()> {
let tempdir = TempDir::new()?;
// Given an extension name, let's fetch its Trunk project
Expand Down Expand Up @@ -164,16 +160,20 @@ impl SubCommand for TestCommand {
};

let obtained = run_psql(&sql_path, &self.connstring)?;
let obtained = obtained.lines().map(remove_psql_message);
let expected = std::fs::read_to_string(expected_file)?;
let diff = TextDiff::from_lines(&expected, &obtained);

for change in diff.iter_all_changes() {
let sign = match change.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Equal => " ",
};
print!("{}{}", sign, change);

for (obtained_line, expected_line) in obtained.zip(expected.lines()) {
let diff = TextDiff::from_lines(obtained_line, expected_line);

for change in diff.iter_all_changes() {
let sign = match change.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Equal => " ",
};
print!("{}{}", sign, change);
}
}
}

Expand Down Expand Up @@ -246,3 +246,15 @@ impl<'a> GitHubProject<'a> {
format!("https://api.github.com/repos/{owner}/{name}/tarball")
}
}

pub fn remove_psql_message(input: &str) -> &str {
if input.starts_with("psql").not() {
return input;
}

let Some((pos, _)) = input.match_indices("ERROR").next() else {
return input;
};

&input[pos..]
}
6 changes: 3 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ enum SubCommands {
Publish(commands::publish::PublishCommand),
/// Install a Postgres extension from the Trunk registry
Install(commands::install::InstallCommand),
/// Test a Postgres extension (coming soon)
Test(commands::test::TestCommand),
/// Test a Postgres extension with its regression tests
Regress(commands::regress::RegressCommand),
}

#[async_trait]
Expand All @@ -45,7 +45,7 @@ impl SubCommand for SubCommands {
SubCommands::Build(cmd) => cmd.execute(task).await,
SubCommands::Publish(cmd) => cmd.execute(task).await,
SubCommands::Install(cmd) => cmd.execute(task).await,
SubCommands::Test(cmd) => cmd.execute(task).await,
SubCommands::Regress(cmd) => cmd.execute(task).await,
}
}
}
Expand Down

0 comments on commit a913e15

Please sign in to comment.