Commit 3d604407 authored by Silvan Mosberger's avatar Silvan Mosberger
Browse files

tests.nixpkgs-check-by-name: Remove error writer

parent e58bc754
Loading
Loading
Loading
Loading
+0 −16
Original line number Diff line number Diff line
use crate::utils::PACKAGE_NIX_FILENAME;
use crate::ErrorWriter;
use itertools::concat;
use itertools::{Either, Itertools};
use rnix::parser::ParseError;
@@ -225,21 +224,6 @@ impl fmt::Display for CheckError {
    }
}

pub fn write_check_result<A, W: io::Write>(
    error_writer: &mut ErrorWriter<W>,
    check_result: CheckResult<A>,
) -> anyhow::Result<Option<A>> {
    match check_result? {
        Either::Left(errors) => {
            for error in errors {
                error_writer.write(&error.to_string())?
            }
            Ok(None)
        }
        Either::Right(value) => Ok(Some(value)),
    }
}

pub fn pass<A>(value: A) -> CheckResult<A> {
    Ok(Either::Right(value))
}
+21 −14
Original line number Diff line number Diff line
@@ -6,13 +6,14 @@ mod utils;

use crate::structure::check_structure;
use anyhow::Context;
use check_result::write_check_result;
use check_result::pass;
use clap::{Parser, ValueEnum};
use colored::Colorize;
use itertools::Either;
use itertools::Either::{Left, Right};
use std::io;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use utils::ErrorWriter;

/// Program to check the validity of pkgs/by-name
#[derive(Parser, Debug)]
@@ -78,26 +79,32 @@ pub fn check_nixpkgs<W: io::Write>(
        nixpkgs_path.display()
    ))?;

    // Wraps the error_writer to print everything in red, and tracks whether anything was printed
    // at all. Later used to figure out if the structure was valid or not.
    let mut error_writer = ErrorWriter::new(error_writer);

    if !nixpkgs_path.join(utils::BASE_SUBPATH).exists() {
    let check_result = if !nixpkgs_path.join(utils::BASE_SUBPATH).exists() {
        eprintln!(
            "Given Nixpkgs path does not contain a {} subdirectory, no check necessary.",
            utils::BASE_SUBPATH
        );
        pass(())
    } else {
        let check_result = check_structure(&nixpkgs_path);

        if let Some(package_names) = write_check_result(&mut error_writer, check_result)? {
        match check_structure(&nixpkgs_path)? {
            Left(errors) => Ok(Left(errors)),
            Right(package_names) =>
            // Only if we could successfully parse the structure, we do the evaluation checks
            let check_result =
                eval::check_values(version, &nixpkgs_path, package_names, eval_accessible_paths);
            write_check_result(&mut error_writer, check_result)?;
            {
                eval::check_values(version, &nixpkgs_path, package_names, eval_accessible_paths)
            }
        }
    };

    match check_result? {
        Either::Left(errors) => {
            for error in errors {
                writeln!(error_writer, "{}", error.to_string().red())?
            }
            Ok(false)
        }
        Either::Right(_) => Ok(true),
    }
    Ok(error_writer.empty)
}

#[cfg(test)]
+0 −24
Original line number Diff line number Diff line
use anyhow::Context;
use colored::Colorize;
use std::fs;
use std::io;
use std::path::Path;
@@ -50,26 +49,3 @@ impl LineIndex {
        }
    }
}

/// A small wrapper around a generic io::Write specifically for errors:
/// - Print everything in red to signal it's an error
/// - Keep track of whether anything was printed at all, so that
///   it can be queried whether any errors were encountered at all
pub struct ErrorWriter<W> {
    pub writer: W,
    pub empty: bool,
}

impl<W: io::Write> ErrorWriter<W> {
    pub fn new(writer: W) -> ErrorWriter<W> {
        ErrorWriter {
            writer,
            empty: true,
        }
    }

    pub fn write(&mut self, string: &str) -> io::Result<()> {
        self.empty = false;
        writeln!(self.writer, "{}", string.red())
    }
}