From 48c9c998705cae537e8d804c405d76481714c01c Mon Sep 17 00:00:00 2001 From: Chi Wang Date: Tue, 28 Sep 2021 11:48:59 +0800 Subject: [PATCH] Agent: Change base path of testlogs from outputPath to BINDIR (#1230) --- agent/src/artifact/mod.rs | 2 +- agent/src/artifact/upload.rs | 7 +++++++ agent/src/lib.rs | 3 ++- agent/src/utils.rs | 35 ++++++++++++++++++++++++++++++++++ agent/tests/artifact/mod.rs | 2 +- agent/tests/artifact/upload.rs | 4 ++-- agent/tests/tests.rs | 2 +- 7 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 agent/src/utils.rs diff --git a/agent/src/artifact/mod.rs b/agent/src/artifact/mod.rs index 3a2200b232..709ebbad0f 100644 --- a/agent/src/artifact/mod.rs +++ b/agent/src/artifact/mod.rs @@ -1 +1 @@ -pub mod upload; \ No newline at end of file +pub mod upload; diff --git a/agent/src/artifact/upload.rs b/agent/src/artifact/upload.rs index 224310811e..be8be49fcc 100644 --- a/agent/src/artifact/upload.rs +++ b/agent/src/artifact/upload.rs @@ -11,6 +11,8 @@ use std::{ }; use tracing::error; +use crate::utils::split_path_inclusive; + #[derive(Clone, Copy, PartialEq, Eq)] pub enum Mode { // Upload as Buildkite's artifacts @@ -217,6 +219,11 @@ fn upload_test_log( mode: Mode, ) -> Result<()> { let path = uri_to_file_path(test_log)?; + + if let Some((first, second)) = split_path_inclusive(&path, "testlogs") { + return upload_artifact(dry, Some(&first), &second, mode); + } + let artifact = if let Some(local_exec_root) = local_exec_root { if let Ok(relative_path) = path.strip_prefix(local_exec_root) { relative_path diff --git a/agent/src/lib.rs b/agent/src/lib.rs index a876aa4a5f..4186d5c784 100644 --- a/agent/src/lib.rs +++ b/agent/src/lib.rs @@ -1 +1,2 @@ -pub mod artifact; \ No newline at end of file +pub mod artifact; +pub mod utils; diff --git a/agent/src/utils.rs b/agent/src/utils.rs new file mode 100644 index 0000000000..803b93dad9 --- /dev/null +++ b/agent/src/utils.rs @@ -0,0 +1,35 @@ +use std::path::{Path, PathBuf}; + +/// Splits [`Path`] into two parts separated by `target`. The `target` itself is included +/// in the end of first part. +/// +/// ``` +/// # use std::path::Path; +/// # use bazelci_agent::utils::split_path_inclusive; +/// +/// let path = Path::new("a/b/c"); +/// let (first, second) = split_path_inclusive(path, "b").unwrap(); +/// assert_eq!(first, Path::new("a/b")); +/// assert_eq!(second, Path::new("c")); +/// ``` +/// +pub fn split_path_inclusive(path: &Path, target: &str) -> Option<(PathBuf, PathBuf)> { + let mut iter = path.iter(); + + let mut first = PathBuf::new(); + let mut found = false; + while let Some(comp) = iter.next() { + first.push(Path::new(comp)); + if comp == target { + found = true; + break; + } + } + + if found { + let second: PathBuf = iter.collect(); + Some((first, second)) + } else { + None + } +} diff --git a/agent/tests/artifact/mod.rs b/agent/tests/artifact/mod.rs index e1ed694e76..f8802ec1d8 100644 --- a/agent/tests/artifact/mod.rs +++ b/agent/tests/artifact/mod.rs @@ -1 +1 @@ -mod upload; \ No newline at end of file +mod upload; diff --git a/agent/tests/artifact/upload.rs b/agent/tests/artifact/upload.rs index dc51f7bee2..c2acd4bc80 100644 --- a/agent/tests/artifact/upload.rs +++ b/agent/tests/artifact/upload.rs @@ -15,7 +15,7 @@ fn test_logs_uploaded_to_buildkite() -> Result<()> { ]); cmd.assert() .success() - .stdout(predicates::str::contains("buildkite-agent artifact upload bazel-out\\x64_windows-fastbuild\\testlogs\\src\\test\\shell\\bazel\\resource_compiler_toolchain_test\\test.log")); + .stdout(predicates::str::contains("buildkite-agent artifact upload src\\test\\shell\\bazel\\resource_compiler_toolchain_test\\test.log")); Ok(()) } @@ -33,7 +33,7 @@ fn test_logs_uploaded_to_buildkite() -> Result<()> { ]); cmd.assert() .success() - .stdout(predicates::str::contains("buildkite-agent artifact upload bazel-out/darwin-fastbuild/testlogs/src/test/shell/bazel/starlark_repository_test/shard_4_of_6/test_attempts/attempt_1.log")); + .stdout(predicates::str::contains("buildkite-agent artifact upload src/test/shell/bazel/starlark_repository_test/shard_4_of_6/test_attempts/attempt_1.log")); Ok(()) } diff --git a/agent/tests/tests.rs b/agent/tests/tests.rs index 26d246fa86..466a2b2690 100644 --- a/agent/tests/tests.rs +++ b/agent/tests/tests.rs @@ -1 +1 @@ -mod artifact; \ No newline at end of file +mod artifact;