gitbucket-cli 0.8.0

A command-line tool for GitBucket, inspired by GitHub CLI
#![allow(dead_code)]

use std::io::Write;
use std::path::Path;
use std::process::{Child, Command, Output, Stdio};

use tempfile::TempDir;

pub fn gb_command() -> std::process::Command {
    assert_cmd::cargo::CommandCargoExt::cargo_bin("gb").unwrap()
}

pub struct GbTestEnv {
    temp: TempDir,
}

impl GbTestEnv {
    pub fn new() -> Self {
        Self {
            temp: tempfile::tempdir().unwrap(),
        }
    }

    pub fn path(&self) -> &Path {
        self.temp.path()
    }

    pub fn command(&self) -> Command {
        let mut command = gb_command();
        for name in [
            "GB_HTTP_CONNECT_TIMEOUT",
            "GB_HTTP_READ_TIMEOUT",
            "GB_HTTP_TIMEOUT",
            "GB_GIT_TIMEOUT",
            "GB_PAGINATION_MAX_PAGES",
        ] {
            command.env_remove(name);
        }
        command
            .current_dir(self.path())
            .env("GB_CONFIG_DIR", self.path())
            .env("NO_COLOR", "1")
            .env_remove("GB_WEB_USER");
        command
    }

    pub fn api_command(&self, host: impl AsRef<str>) -> Command {
        let mut command = self.command();
        command
            .env("GB_HOST", host.as_ref())
            .env("GB_TOKEN", "test-token")
            .env("GB_PROTOCOL", "http");
        command
    }

    pub fn repo_api_command(&self, host: impl AsRef<str>, repo: impl AsRef<str>) -> Command {
        let mut command = self.api_command(host);
        command.env("GB_REPO", repo.as_ref());
        command
    }
}

pub fn assert_success(output: &Output) {
    assert!(
        output.status.success(),
        "stdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

pub fn assert_failure(output: &Output) {
    assert!(
        !output.status.success(),
        "stdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

pub fn stdout(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

pub fn stderr(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).to_string()
}

pub fn spawn_with_stdin(command: &mut Command, input: &[u8]) -> Child {
    let mut child = command
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child.stdin.take().unwrap().write_all(input).unwrap();
    child
}

pub fn with_stdin(command: &mut Command, input: &[u8]) -> Output {
    spawn_with_stdin(command, input).wait_with_output().unwrap()
}