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

Add symlink parameter #468

Merged
merged 16 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion crates/weaver_codegen_test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const SEMCONV_REGISTRY_PATH: &str = "./semconv_registry/";
const TEMPLATES_PATH: &str = "./templates/registry/";
const REGISTRY_ID: &str = "test";
const TARGET: &str = "rust";
const FOLLOW_SYMLINKS: bool = false;

fn main() {
// Tell Cargo when to rerun this build script
Expand All @@ -45,7 +46,7 @@ fn main() {
};
let registry_repo =
RegistryRepo::try_new("main", &registry_path).unwrap_or_else(|e| process_error(&logger, e));
let semconv_specs = SchemaResolver::load_semconv_specs(&registry_repo)
let semconv_specs = SchemaResolver::load_semconv_specs(&registry_repo, FOLLOW_SYMLINKS)
.ignore(|e| matches!(e.severity(), Some(miette::Severity::Warning)))
.into_result_failing_non_fatal()
.unwrap_or_else(|e| process_error(&logger, e));
Expand Down
4 changes: 4 additions & 0 deletions crates/weaver_resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ impl SchemaResolver {
/// * `registry_repo` - The registry repository containing the semantic convention files.
pub fn load_semconv_specs(
registry_repo: &RegistryRepo,
follow_symlinks: bool,
) -> WResult<Vec<(String, SemConvSpec)>, weaver_semconv::Error> {
Self::load_semconv_from_local_path(
registry_repo.path().to_path_buf(),
registry_repo.registry_path_repr(),
follow_symlinks,
)
}

Expand All @@ -285,6 +287,7 @@ impl SchemaResolver {
fn load_semconv_from_local_path(
local_path: PathBuf,
registry_path_repr: &str,
follow_symlinks: bool,
) -> WResult<Vec<(String, SemConvSpec)>, weaver_semconv::Error> {
fn is_hidden(entry: &DirEntry) -> bool {
entry
Expand All @@ -306,6 +309,7 @@ impl SchemaResolver {
// All yaml files are recursively loaded and parsed in parallel from
// the given path.
let result = walkdir::WalkDir::new(local_path.clone())
.follow_links(follow_symlinks)
.into_iter()
.filter_entry(|e| !is_hidden(e))
.par_bridge()
Expand Down
18 changes: 14 additions & 4 deletions crates/weaver_semconv_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,13 @@ impl SnippetGenerator {
registry_repo: &RegistryRepo,
template_engine: TemplateEngine,
diag_msgs: &mut DiagnosticMessages,
follow_symlinks: bool,
) -> Result<SnippetGenerator, Error> {
let registry = ResolvedSemconvRegistry::try_from_registry_repo(registry_repo, diag_msgs)?;
let registry = ResolvedSemconvRegistry::try_from_registry_repo(
registry_repo,
diag_msgs,
follow_symlinks,
)?;
Ok(SnippetGenerator {
lookup: registry,
template_engine,
Expand All @@ -327,9 +332,10 @@ impl ResolvedSemconvRegistry {
fn try_from_registry_repo(
registry_repo: &RegistryRepo,
diag_msgs: &mut DiagnosticMessages,
follow_symlinks: bool,
) -> Result<ResolvedSemconvRegistry, Error> {
let registry_id = "semantic_conventions";
let semconv_specs = SchemaResolver::load_semconv_specs(registry_repo)
let semconv_specs = SchemaResolver::load_semconv_specs(registry_repo, follow_symlinks)
.capture_non_fatal_errors(diag_msgs)?;
let mut registry = SemConvRegistry::from_semconv_specs(registry_id, semconv_specs);
let schema = SchemaResolver::resolve_semantic_convention_registry(&mut registry)?;
Expand Down Expand Up @@ -382,8 +388,12 @@ mod tests {
};
let mut diag_msgs = DiagnosticMessages::empty();
let registry_repo = RegistryRepo::try_new("main", &registry_path)?;
let generator =
SnippetGenerator::try_from_registry_repo(&registry_repo, template, &mut diag_msgs)?;
let generator = SnippetGenerator::try_from_registry_repo(
&registry_repo,
template,
&mut diag_msgs,
false,
)?;
let attribute_registry_url = "/docs/attributes-registry";
// Now we should check a snippet.
let test = "data/templates.md";
Expand Down
1 change: 1 addition & 0 deletions data/symbolic_test/semconv_registry
36 changes: 29 additions & 7 deletions src/registry/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use weaver_common::Logger;
use weaver_forge::registry::ResolvedRegistry;
use weaver_semconv::registry::SemConvRegistry;

use crate::registry::RegistryArgs;
use crate::registry::{CommonRegistryArgs, RegistryArgs};
use crate::util::{
check_policy, check_policy_stage, init_policy_engine, load_semconv_specs, resolve_semconv_specs,
};
Expand Down Expand Up @@ -47,6 +47,10 @@ pub struct RegistryCheckArgs {
/// Parameters to specify the diagnostic format.
#[command(flatten)]
pub diagnostic: DiagnosticArgs,

/// Common weaver registry parameters
#[command(flatten)]
pub common_registry_args: CommonRegistryArgs,
}

/// Check a semantic convention registry.
Expand Down Expand Up @@ -78,17 +82,25 @@ pub(crate) fn command(

// Load the semantic convention registry into a local registry repo.
// No parsing errors should be observed.
let main_semconv_specs = load_semconv_specs(&main_registry_repo, logger.clone())
.capture_non_fatal_errors(&mut diag_msgs)?;
let main_semconv_specs = load_semconv_specs(
&main_registry_repo,
logger.clone(),
args.common_registry_args.follow_symlinks,
)
.capture_non_fatal_errors(&mut diag_msgs)?;
let baseline_semconv_specs = baseline_registry_repo
.as_ref()
.map(|repo| {
// Baseline registry resolution should allow non-future features
// and warnings against it should be suppressed when evaluating
// against it as a "baseline".
load_semconv_specs(repo, logger.clone())
.ignore(|e| matches!(e.severity(), Some(miette::Severity::Warning)))
.capture_non_fatal_errors(&mut diag_msgs)
load_semconv_specs(
repo,
logger.clone(),
args.common_registry_args.follow_symlinks,
)
.ignore(|e| matches!(e.severity(), Some(miette::Severity::Warning)))
.capture_non_fatal_errors(&mut diag_msgs)
})
.transpose()?;

Expand Down Expand Up @@ -219,7 +231,8 @@ mod tests {
use crate::cli::{Cli, Commands};
use crate::registry::check::RegistryCheckArgs;
use crate::registry::{
semconv_registry, RegistryArgs, RegistryCommand, RegistryPath, RegistrySubCommand,
semconv_registry, CommonRegistryArgs, RegistryArgs, RegistryCommand, RegistryPath,
RegistrySubCommand,
};
use crate::run_command;

Expand All @@ -243,6 +256,9 @@ mod tests {
skip_policies: true,
display_policy_coverage: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs {
follow_symlinks: false,
},
}),
})),
};
Expand All @@ -269,6 +285,9 @@ mod tests {
skip_policies: false,
display_policy_coverage: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs {
follow_symlinks: false,
},
}),
})),
};
Expand All @@ -295,6 +314,9 @@ mod tests {
skip_policies: false,
display_policy_coverage: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs {
follow_symlinks: false,
},
}),
};

Expand Down
126 changes: 121 additions & 5 deletions src/registry/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use weaver_forge::registry::ResolvedRegistry;
use weaver_forge::{OutputDirective, TemplateEngine, SEMCONV_JQ};
use weaver_semconv::registry::SemConvRegistry;

use crate::registry::{Error, RegistryArgs};
use crate::registry::{CommonRegistryArgs, Error, RegistryArgs};
use crate::util::{check_policy, init_policy_engine, load_semconv_specs, resolve_semconv_specs};
use crate::{registry, DiagnosticArgs, ExitDirectives};

Expand Down Expand Up @@ -73,6 +73,10 @@ pub struct RegistryGenerateArgs {
/// Parameters to specify the diagnostic format.
#[command(flatten)]
pub diagnostic: DiagnosticArgs,

/// Common weaver registry parameters
#[command(flatten)]
pub common_registry_args: CommonRegistryArgs,
}

/// Utility function to parse key-value pairs from the command line.
Expand Down Expand Up @@ -114,9 +118,13 @@ pub(crate) fn command(
let registry_repo = RegistryRepo::try_new("main", &registry_path)?;

// Load the semantic convention registry into a local cache.
let semconv_specs = load_semconv_specs(&registry_repo, logger.clone())
.ignore(|e| matches!(e.severity(), Some(miette::Severity::Warning)))
.into_result_failing_non_fatal()?;
let semconv_specs = load_semconv_specs(
&registry_repo,
logger.clone(),
args.common_registry_args.follow_symlinks,
)
.ignore(|e| matches!(e.severity(), Some(miette::Severity::Warning)))
.into_result_failing_non_fatal()?;

if !args.skip_policies {
let policy_engine = init_policy_engine(&registry_repo, &args.policies, false)?;
Expand Down Expand Up @@ -209,7 +217,9 @@ mod tests {

use crate::cli::{Cli, Commands};
use crate::registry::generate::RegistryGenerateArgs;
use crate::registry::{RegistryArgs, RegistryCommand, RegistryPath, RegistrySubCommand};
use crate::registry::{
CommonRegistryArgs, RegistryArgs, RegistryCommand, RegistryPath, RegistrySubCommand,
};
use crate::run_command;

#[test]
Expand Down Expand Up @@ -240,6 +250,9 @@ mod tests {
skip_policies: true,
future: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs {
follow_symlinks: false,
},
}),
})),
};
Expand Down Expand Up @@ -312,6 +325,9 @@ mod tests {
skip_policies: false,
future: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs {
follow_symlinks: false,
},
}),
})),
};
Expand Down Expand Up @@ -356,6 +372,9 @@ mod tests {
skip_policies: true,
future: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs {
follow_symlinks: false,
},
}),
})),
};
Expand Down Expand Up @@ -402,4 +421,101 @@ mod tests {

assert_eq!(rust_files, expected_rust_files);
}

#[test]
fn test_registry_generate_with_symbolic_link_cases() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue I’m facing is having a unit test in the main crate that depends on test data from another crate (crates/weaver_codegen_test). I think it would be better to have this symbolic link in the data directory of the main crate to avoid this kind of dependency, which might cause problems at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out! The reason I created the symbolic link in crates/weaver_codegen_test is because, based on the function test_registry_generate, it appears to rely on the registry located in crates/weaver_codegen_test/semconv_registry/.

I think this indicates that the test depends on the weaver_codegen_test crate to access the registry. To ensure the required dependencies are properly linked, I created the symbolic link there to align with this structure.

But I have created the commit to reflect the suggestion you recommend, PTAL f985907 @lquerel

let test_cases = vec![
(
true, // follow_symlinks
vec![
// expected files when following symlinks
"attributes/client.rs",
"metrics/system.rs",
"attributes/mod.rs",
"metrics/http.rs",
"attributes/exception.rs",
"attributes/server.rs",
"metrics/mod.rs",
"attributes/network.rs",
"attributes/url.rs",
"attributes/http.rs",
"attributes/system.rs",
"attributes/error.rs",
],
),
(
false, // don't follow_symlinks
vec![], // expect no files when not following symlinks
),
];

for (follow_symlinks, expected_files) in test_cases {
let logger = TestLogger::new();
let temp_output = TempDir::new("output")
.expect("Failed to create temporary directory")
.into_path();

let cli = Cli {
debug: 0,
quiet: false,
future: false,
command: Some(Commands::Registry(RegistryCommand {
command: RegistrySubCommand::Generate(RegistryGenerateArgs {
target: "rust".to_owned(),
output: temp_output.clone(),
templates: PathBuf::from("crates/weaver_codegen_test/templates/"),
config: None,
param: None,
params: None,
registry: RegistryArgs {
registry: RegistryPath::LocalFolder {
path: "data/symbolic_test/".to_owned(),
},
registry_git_sub_dir: None,
},
policies: vec![],
skip_policies: true,
future: false,
diagnostic: Default::default(),
common_registry_args: CommonRegistryArgs { follow_symlinks },
}),
})),
};

let exit_directive = run_command(&cli, logger.clone());
// The command should succeed in both cases
assert_eq!(exit_directive.exit_code, 0);

// Get the actual generated files
let rust_files: std::collections::HashSet<_> = walkdir::WalkDir::new(&temp_output)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().map_or(false, |ext| ext == "rs"))
.map(|e| {
e.path()
.strip_prefix(&temp_output)
.unwrap()
.to_string_lossy()
.to_string()
})
.collect();

// Convert expected files to paths with proper OS separators
let expected_rust_files: std::collections::HashSet<_> = expected_files
.into_iter()
.map(|s| {
s.split('/')
.collect::<PathBuf>()
.to_string_lossy()
.to_string()
})
.collect();

assert_eq!(
rust_files, expected_rust_files,
"File sets don't match for follow_symlinks = {}",
follow_symlinks
);
}
}
}
11 changes: 11 additions & 0 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,14 @@ pub fn semconv_registry(log: impl Logger + Sync + Clone, command: &RegistryComma
),
}
}

/// Set of Parameters used to specify the extra options for the `weaver registry` command.
/// The CommonRegistryArgs will be shared across all `weaver registry` sub-commands. So only the general options should be
/// included here.
#[derive(Args, Debug)]
pub struct CommonRegistryArgs {
/// Boolean flag to specify whether to follow symlinks when loading the registry.
/// Default is false.
#[arg(short, long)]
pub(crate) follow_symlinks: bool,
}
Loading
Loading