Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: improve commands on windows to return stdout and stderr #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions 22 .github/workflows/clear-caches.yml

This file was deleted.

91 changes: 40 additions & 51 deletions 91 postgresql_commands/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::error::{Error, Result};
use std::env::consts::OS;
use std::ffi::{OsStr, OsString};
use std::fmt::Debug;
use std::path::PathBuf;
use std::process::ExitStatus;
use std::time::Duration;
use tracing::debug;

Expand Down Expand Up @@ -140,46 +142,37 @@
/// Execute the command and return the stdout and stderr
fn execute(&mut self) -> Result<(String, String)> {
debug!("Executing command: {}", self.to_command_string());
#[cfg(not(target_os = "windows"))]
{
let output = self.output()?;
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();

debug!(
"Result: {}\nstdout: {}\nstderr: {}",
output
.status
.code()
.map_or("None".to_string(), |c| c.to_string()),
stdout,
stderr
);

if output.status.success() {
Ok((stdout, stderr))
} else {
Err(Error::CommandError { stdout, stderr })
}
}
let program = self.get_program().to_string_lossy().to_string();
let stdout: String;
let stderr: String;
let status: ExitStatus;

// TODO: Processes can hang on Windows when attempting to get stdout/stderr using code
// that works for Linux/MacOS; this implementation should be updated to retrieve the
// values of stdout/stderr without hanging
#[cfg(target_os = "windows")]
{
if OS == "windows" && program.as_str().ends_with("pg_ctl") {
// The pg_ctl process can hang on Windows when attempting to get stdout/stderr.
let mut process = self
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()?;
let status = process.wait()?;
let stdout = String::new();
let stderr = String::new();
if status.success() {
Ok((stdout, stderr))
} else {
Err(Error::CommandError { stdout, stderr })
}
stdout = String::new();
stderr = String::new();
status = process.wait()?;

Check warning on line 158 in postgresql_commands/src/traits.rs

View check run for this annotation

Codecov / codecov/patch

postgresql_commands/src/traits.rs#L156-L158

Added lines #L156 - L158 were not covered by tests
} else {
let output = self.output()?;
stdout = String::from_utf8_lossy(&output.stdout).into_owned();
stderr = String::from_utf8_lossy(&output.stderr).into_owned();
status = output.status;
}
debug!(
"Result: {}\nstdout: {}\nstderr: {}",
status.code().map_or("None".to_string(), |c| c.to_string()),
stdout,
stderr
);

if status.success() {
Ok((stdout, stderr))
} else {
Err(Error::CommandError { stdout, stderr })

Check warning on line 175 in postgresql_commands/src/traits.rs

View check run for this annotation

Codecov / codecov/patch

postgresql_commands/src/traits.rs#L175

Added line #L175 was not covered by tests
}
}
}
Expand All @@ -194,19 +187,18 @@
Some(duration) => tokio::time::timeout(duration, self.output()).await?,
None => self.output().await,
}?;

#[cfg(not(target_os = "windows"))]
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
#[cfg(not(target_os = "windows"))]
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();

// TODO: Processes can hang on Windows when attempting to get stdout/stderr using code
// that works for Linux/MacOS; this implementation should be updated to retrieve the
// values of stdout/stderr without hanging
#[cfg(target_os = "windows")]
let stdout = String::new();
#[cfg(target_os = "windows")]
let stderr = String::new();
let program = self.as_std().get_program().to_string_lossy().to_string();
let stdout: String;
let stderr: String;

if OS == "windows" && program.as_str().ends_with("pg_ctl") {
// The pg_ctl process can hang on Windows when attempting to get stdout/stderr.
stdout = String::new();
stderr = String::new();

Check warning on line 197 in postgresql_commands/src/traits.rs

View check run for this annotation

Codecov / codecov/patch

postgresql_commands/src/traits.rs#L195-L197

Added lines #L195 - L197 were not covered by tests
} else {
stdout = String::from_utf8_lossy(&output.stdout).into_owned();
stderr = String::from_utf8_lossy(&output.stderr).into_owned();
}

debug!(
"Result: {}\nstdout: {}\nstderr: {}",
Expand Down Expand Up @@ -371,10 +363,7 @@
command.args(["/C", "echo foo"]);

let (stdout, stderr) = command.execute()?;
#[cfg(not(target_os = "windows"))]
assert!(stdout.starts_with("foo"));
#[cfg(target_os = "windows")]
assert!(stdout.is_empty());
assert!(stderr.is_empty());
Ok(())
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.