Skip to content

Commit 986a5b1

Browse files
committed
✨ --validate
- Added --validate and --validate-file flags in Cyclonedx::BomBuilder. - After writing the BOM, if --validate is set, validate JSON via JSON Schema and XML via XSD with local files under schema/. - Added logic to validate an existing file with --validate --validate-file <path>, inferring format from extension unless --format is provided.</path> - In validate-only mode, project path isn’t required. - Added Cyclonedx::BomHelpers.validate_bom_content(content, format, spec_version) which: - For JSON: uses json_schemer to validate against bom-<ver>.schema.json.</ver> - For XML: uses Nokogiri::XML::Schema with bom-<ver>.xsd.</ver> - Uses local schemas at schema/ and surfaces compact error messages; returns non-zero exit on failure. - Added json_schemer (~> 2.2) to cyclonedx-ruby.gemspec. - Required json_schemer in lib/cyclonedx/ruby.rb. - Updated features/help.feature to show the new flags. - Added features/validate.feature: - Validate XML BOM succeeds. - Validate JSON BOM succeeds. - Validate fails for invalid XML BOM (corrupts namespace and expects exit 1). - Infer format from file extension when using --validate-file and no --format provided. Signed-off-by: Peter H. Boling <[email protected]>
1 parent de1c733 commit 986a5b1

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lib/cyclonedx/bom_builder.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,17 @@ def self.setup(path)
142142
end
143143

144144
# Normalize to an absolute project path to avoid relative path issues later
145-
@project_path = File.expand_path(@options[:path])
145+
@project_path = File.expand_path(@options[:path]) if @options[:path]
146146
@provided_path = @options[:path]
147147

148-
begin
149-
@logger.info("Changing directory to Ruby project directory located at #{@provided_path}")
150-
Dir.chdir @project_path
151-
rescue StandardError => e
152-
@logger.error("Unable to change directory to Ruby project directory located at #{@provided_path}. #{e.message}: #{Array(e.backtrace).join("\n")}")
153-
abort
148+
if @project_path
149+
begin
150+
@logger.info("Changing directory to Ruby project directory located at #{@provided_path}")
151+
Dir.chdir @project_path
152+
rescue StandardError => e
153+
@logger.error("Unable to change directory to Ruby project directory located at #{@provided_path}. #{e.message}: #{Array(e.backtrace).join("\n")}")
154+
abort
155+
end
154156
end
155157

156158
if @options[:bom_output_format].nil?
@@ -191,9 +193,9 @@ def self.setup(path)
191193
if @project_path
192194
begin
193195
# Use absolute path so it's correct regardless of current working directory
194-
gemfile_path = File.join(@project_path, 'Gemfile.lock')
195-
# Compute display path for logs: './Gemfile.lock' when provided path is '.', else '<provided>/Gemfile.lock'
196-
display_gemfile_path = (@provided_path == '.' ? './Gemfile.lock' : File.join(@provided_path, 'Gemfile.lock'))
196+
gemfile_path = File.join(@project_path, 'Gemfile.lock')
197+
# Compute display path for logs: './Gemfile.lock' when provided path is '.', else '<provided>/Gemfile.lock'
198+
display_gemfile_path = (@provided_path == '.' ? './Gemfile.lock' : File.join(@provided_path, 'Gemfile.lock'))
197199
@logger.info("Parsing specs from #{display_gemfile_path}...")
198200
gemfile_contents = File.read(gemfile_path)
199201
@specs = Bundler::LockfileParser.new(gemfile_contents).specs

0 commit comments

Comments
 (0)