Skip to content
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

Make msbuild verbosity level configurable #74

Merged
merged 7 commits into from
Jul 19, 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
89 changes: 89 additions & 0 deletions buildpacks/dotnet/src/dotnet_buildpack_configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::dotnet_publish_command::VerbosityLevel;

pub(crate) struct DotnetBuildpackConfiguration {
pub(crate) msbuild_verbosity_level: VerbosityLevel,
}

#[derive(Debug, PartialEq)]
pub(crate) enum DotnetBuildpackConfigurationError {
InvalidMsbuildVerbosityLevel(String),
}

impl TryFrom<&libcnb::Env> for DotnetBuildpackConfiguration {
type Error = DotnetBuildpackConfigurationError;

fn try_from(env: &libcnb::Env) -> Result<Self, Self::Error> {
Ok(Self {
msbuild_verbosity_level: detect_msbuild_verbosity_level(env)?,
})
}
}

fn detect_msbuild_verbosity_level(
env: &libcnb::Env,
) -> Result<VerbosityLevel, DotnetBuildpackConfigurationError> {
env.get("MSBUILD_VERBOSITY_LEVEL")
.map(|value| value.to_string_lossy())
.map_or(Ok(VerbosityLevel::Minimal), |value| {
match value.to_lowercase().as_str() {
"q" | "quiet" => Ok(VerbosityLevel::Quiet),
"m" | "minimal" => Ok(VerbosityLevel::Minimal),
"n" | "normal" => Ok(VerbosityLevel::Normal),
"d" | "detailed" => Ok(VerbosityLevel::Detailed),
"diag" | "diagnostic" => Ok(VerbosityLevel::Diagnostic),
_ => Err(
DotnetBuildpackConfigurationError::InvalidMsbuildVerbosityLevel(
value.to_string(),
),
),
}
})
}

#[cfg(test)]
mod tests {
use super::*;
use libcnb::Env;

fn create_env(variables: &[(&str, &str)]) -> Env {
let mut env = Env::new();
for &(key, value) in variables {
env.insert(key, value);
}
env
}

#[test]
fn test_detect_msbuild_verbosity_level() {
let cases = [
(Some("quiet"), Ok(VerbosityLevel::Quiet)),
(Some("q"), Ok(VerbosityLevel::Quiet)),
(Some("minimal"), Ok(VerbosityLevel::Minimal)),
(Some("m"), Ok(VerbosityLevel::Minimal)),
(Some("normal"), Ok(VerbosityLevel::Normal)),
(Some("n"), Ok(VerbosityLevel::Normal)),
(Some("detailed"), Ok(VerbosityLevel::Detailed)),
(Some("d"), Ok(VerbosityLevel::Detailed)),
(Some("diagnostic"), Ok(VerbosityLevel::Diagnostic)),
(Some("diag"), Ok(VerbosityLevel::Diagnostic)),
(
Some("invalid"),
Err(
DotnetBuildpackConfigurationError::InvalidMsbuildVerbosityLevel(
"invalid".to_string(),
),
),
),
(None, Ok(VerbosityLevel::Minimal)),
];

for (input, expected) in &cases {
let env = match input {
Some(value) => create_env(&[("MSBUILD_VERBOSITY_LEVEL", value)]),
None => Env::new(),
};
let result = detect_msbuild_verbosity_level(&env);
assert_eq!(result, *expected);
}
}
}
3 changes: 1 addition & 2 deletions buildpacks/dotnet/src/dotnet_publish_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ impl From<DotnetPublishCommand> for Command {
}
}

#[derive(Clone, Copy)]
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum VerbosityLevel {
Quiet,
Minimal,
Expand Down
22 changes: 22 additions & 0 deletions buildpacks/dotnet/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::dotnet::target_framework_moniker::ParseTargetFrameworkError;
use crate::dotnet::{project, solution};
use crate::dotnet_buildpack_configuration::DotnetBuildpackConfigurationError;
use crate::launch_process::LaunchProcessDetectionError;
use crate::layers::sdk::SdkLayerError;
use crate::utils::StreamedCommandError;
Expand Down Expand Up @@ -157,6 +158,27 @@ fn on_buildpack_error(error: &DotnetBuildpackError) {
io_error,
),
},
DotnetBuildpackError::ParseDotnetBuildpackConfigurationError(error) => match error {
DotnetBuildpackConfigurationError::InvalidMsbuildVerbosityLevel(verbosity_level) => {
log_error(
"Invalid MSBuild verbosity level",
formatdoc! {"
The 'MSBUILD_VERBOSITY_LEVEL' environment variable value ('{verbosity_level}') could not be parsed. Did you mean one of the following?

d
detailed
diag
diagnostic
m
minimal
n
normal
q
quiet
"},
);
}
},
DotnetBuildpackError::PublishCommand(error) => match error {
StreamedCommandError::Io(io_error) => log_io_error(
"Unable to publish .NET file",
Expand Down
12 changes: 10 additions & 2 deletions buildpacks/dotnet/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod detect;
mod dotnet;
mod dotnet_buildpack_configuration;
mod dotnet_layer_env;
mod dotnet_publish_command;
mod errors;
Expand All @@ -12,7 +13,10 @@ use crate::dotnet::project::Project;
use crate::dotnet::runtime_identifier;
use crate::dotnet::solution::Solution;
use crate::dotnet::target_framework_moniker::{ParseTargetFrameworkError, TargetFrameworkMoniker};
use crate::dotnet_publish_command::{DotnetPublishCommand, VerbosityLevel};
use crate::dotnet_buildpack_configuration::{
DotnetBuildpackConfiguration, DotnetBuildpackConfigurationError,
};
use crate::dotnet_publish_command::DotnetPublishCommand;
use crate::launch_process::LaunchProcessDetectionError;
use crate::layers::sdk::SdkLayerError;
use crate::utils::StreamedCommandError;
Expand Down Expand Up @@ -109,12 +113,15 @@ impl Buildpack for DotnetBuildpack {
)
.map_err(DotnetBuildpackError::LaunchProcessDetection);

let buildpack_configuration = DotnetBuildpackConfiguration::try_from(&Env::from_current())
.map_err(DotnetBuildpackError::ParseDotnetBuildpackConfigurationError)?;

utils::run_command_and_stream_output(
Command::from(DotnetPublishCommand {
path: solution.path,
configuration: build_configuration,
runtime_identifier,
verbosity_level: VerbosityLevel::Normal,
verbosity_level: buildpack_configuration.msbuild_verbosity_level,
})
.current_dir(&context.app_dir)
.envs(&command_env.apply(Scope::Build, &Env::from_current())),
Expand Down Expand Up @@ -244,6 +251,7 @@ enum DotnetBuildpackError {
ParseVersionRequirement(semver::Error),
ResolveSdkVersion(VersionReq),
SdkLayer(SdkLayerError),
ParseDotnetBuildpackConfigurationError(DotnetBuildpackConfigurationError),
PublishCommand(StreamedCommandError),
CopyRuntimeFilesToRuntimeLayer(io::Error),
LaunchProcessDetection(LaunchProcessDetectionError),
Expand Down
5 changes: 2 additions & 3 deletions buildpacks/dotnet/tests/dotnet_publish_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use libcnb_test::{assert_contains, assert_empty, TestRunner};
#[ignore = "integration test"]
fn test_dotnet_publish_with_rid() {
TestRunner::default().build(
default_build_config("tests/fixtures/basic_web_8.0_with_global_json"),
default_build_config("tests/fixtures/basic_web_8.0_with_global_json")
.env("MSBUILD_VERBOSITY_LEVEL", "normal"),
|context| {
assert_empty!(context.pack_stderr);
// TODO: Find a more elegant approach to testing MSBuild logs (and/or just reduce MSbuild verbosity level)

#[cfg(target_arch = "x86_64")]
let arch = "x64";
#[cfg(target_arch = "aarch64")]
Expand Down
3 changes: 2 additions & 1 deletion buildpacks/dotnet/tests/nuget_layer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use libcnb_test::{assert_contains, assert_empty, TestRunner};
#[ignore = "integration test"]
fn test_nuget_restore_and_cache() {
TestRunner::default().build(
default_build_config("tests/fixtures/console_with_nuget_package"),
default_build_config("tests/fixtures/console_with_nuget_package")
.env("MSBUILD_VERBOSITY_LEVEL", "normal"),
|context| {
assert_empty!(context.pack_stderr);
assert_contains!(
Expand Down