From ccef26b373ffbb110b5b4efe446d6959ff0b700c Mon Sep 17 00:00:00 2001 From: Natalia Pozhidaeva Date: Thu, 18 Jan 2024 02:55:35 -0500 Subject: [PATCH 1/5] [#223] Report transformation to JSON --- assets/puzzles_json.xsl | 62 ++++++++++++++++++++++++++++++ bin/pdd | 13 +++++-- features/json_output.feature | 22 +++++++++++ features/step_definitions/steps.rb | 8 ++++ 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 assets/puzzles_json.xsl create mode 100644 features/json_output.feature diff --git a/assets/puzzles_json.xsl b/assets/puzzles_json.xsl new file mode 100644 index 0000000..23ecb35 --- /dev/null +++ b/assets/puzzles_json.xsl @@ -0,0 +1,62 @@ + + + + + + { + + "puzzles": [ + + + ] +} + + + + "version": "", + + "date": "", + + + { + + "id": "", + + "ticket": "", + + "file": "", + + "lines": "", + + "body": "", + + "estimate": "", + + "role": "" + + }, + + + diff --git a/bin/pdd b/bin/pdd index d4d30df..c0a2af3 100755 --- a/bin/pdd +++ b/bin/pdd @@ -59,12 +59,12 @@ begin exit end o.string '-s', '--source', 'Source directory to parse ("." by default)' - o.string '-f', '--file', 'File to save XML into' + o.string '-f', '--file', 'File to save report into' o.array '-e', '--exclude', 'Glob pattern to exclude, e.g. "**/*.jpg"', default: [] o.array '-n', '--include', 'Glob pattern to include, e.g. "**/*.jpg"', default: [] - o.string '-t', '--format', 'Format of the report (xml|html)' + o.string '-t', '--format', 'Format of the report (xml|html|json)' o.array( '-r', '--rule', 'Rule to apply (can be used many times)', delimiter: ';' @@ -105,8 +105,15 @@ https://github.com/cqfn/pdd/blob/master/README.md" 'assets', 'puzzles.xsl' ) output = Nokogiri::XSLT(File.read(xslt)).transform(Nokogiri::XML(xml)) + elsif opts[:format] == 'json' + xslt = File.join( + File.dirname(File.dirname(__FILE__)), + 'assets', 'puzzles_json.xsl' + ) + # result is not xml, so use apply + output = Nokogiri::XSLT(File.read(xslt)).apply_to(Nokogiri::XML(xml)) elsif opts[:format] != 'xml' - raise 'Invalid format, use html or xml' + raise 'Invalid format, use html or xml or json' end end file << output diff --git a/features/json_output.feature b/features/json_output.feature new file mode 100644 index 0000000..7e87bd4 --- /dev/null +++ b/features/json_output.feature @@ -0,0 +1,22 @@ +Feature: JSON output + As a source code writer I want to be able to + call PDD as a command line tool, and retrieve an + JSON report + + Scenario: JSON report building + Given I have a "Sample.java" file with content: + """ + public class Main { + /** + * @todo #13 Let's do json + * or maybe not json ":)" + */ + public void main(String[] args) { + // later + } + } + """ + When I run bin/pdd with "-v -s . -f out.json --format=json" + Then Exit code is zero + And Stdout contains "Reading from root dir ." + And Text File "out.json" contains "Let's do json or maybe not json “:)“" diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb index fd44afc..0e733d2 100644 --- a/features/step_definitions/steps.rb +++ b/features/step_definitions/steps.rb @@ -95,6 +95,14 @@ raise "XML file #{file} doesn't match \"#{xpath}\":\n#{xml}" if xml.xpath(xpath).empty? end +Then(/^Text File "([^"]+)" contains "([^"]+)"$/) do |file, substring| + raise "File #{file} doesn't exit" unless File.exist?(file) + + content = File.read(file) + raise "File #{file} doesn't contain \"#{substring}\":\n#{content}" \ + if content.index(substring).nil? +end + Then(/^Exit code is zero$/) do raise "Non-zero exit code #{@exitstatus}" unless @exitstatus.zero? end From 6dee67408a136335cc73ce1f23e0c14a4d6c5605 Mon Sep 17 00:00:00 2001 From: Natalia Pozhidaeva Date: Thu, 18 Jan 2024 03:03:03 -0500 Subject: [PATCH 2/5] [#223] Add checking parameters --- Rakefile | 3 +++ cucumber.yml | 1 + 2 files changed, 4 insertions(+) diff --git a/Rakefile b/Rakefile index 3ec038d..bc476df 100644 --- a/Rakefile +++ b/Rakefile @@ -80,6 +80,9 @@ end Cucumber::Rake::Task.new(:'features:html') do |t| t.profile = 'html_report' end +Cucumber::Rake::Task.new(:'features:json') do |t| + t.profile = 'json_report' +end task :copyright do sh "grep -q -r '2014-#{Date.today.strftime('%Y')}' \ diff --git a/cucumber.yml b/cucumber.yml index 78342a1..020d89f 100644 --- a/cucumber.yml +++ b/cucumber.yml @@ -1,3 +1,4 @@ default: --format pretty travis: --format progress html_report: --format progress --format html --out=features_report.html +json_report: --format progress --format json --out=features_report.json From 13d2842a6763b00472a43da0b17202b51012531a Mon Sep 17 00:00:00 2001 From: Natalia Pozhidaeva Date: Thu, 18 Jan 2024 03:31:51 -0500 Subject: [PATCH 3/5] [#223] Add new feachure file to ignore --- .pdd | 1 + 1 file changed, 1 insertion(+) diff --git a/.pdd b/.pdd index f94195c..39b7d2d 100644 --- a/.pdd +++ b/.pdd @@ -12,6 +12,7 @@ --exclude features/remove.feature --exclude features/uses_config.feature --exclude features/html_output.feature +--exclude features/json_output.feature --exclude features/avoiding_duplicates.feature --exclude features/applies_rules.feature --exclude features/unicode.feature From 0f9fc5b884c278f0b64671632dca5bc855451b4a Mon Sep 17 00:00:00 2001 From: Natalia Pozhidaeva Date: Thu, 18 Jan 2024 03:48:06 -0500 Subject: [PATCH 4/5] [#223] FIx xsl formatting for style checker --- assets/puzzles_json.xsl | 45 ++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/assets/puzzles_json.xsl b/assets/puzzles_json.xsl index 23ecb35..4b04a5e 100644 --- a/assets/puzzles_json.xsl +++ b/assets/puzzles_json.xsl @@ -24,9 +24,7 @@ SOFTWARE. --> - - { - + { "puzzles": [ @@ -35,28 +33,47 @@ SOFTWARE. - "version": "", + "version": " + + ", - "date": "", + "date": " + + ", - { + { - "id": "", + "id": " + + ", - "ticket": "", + "ticket": " + + ", - "file": "", + "file": " + + ", - "lines": "", + "lines": " + + ", - "body": "", + "body": " + + ", - "estimate": "", + "estimate": " + + ", - "role": "" + "role": " + + " - }, + } + , From 93eb0d3fbf70a66472c6ec277c2cc0a0538763dc Mon Sep 17 00:00:00 2001 From: Natalia Pozhidaeva Date: Thu, 18 Jan 2024 09:21:12 -0500 Subject: [PATCH 5/5] Update features/step_definitions/steps.rb Co-authored-by: Slava Semushin --- features/step_definitions/steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb index 0e733d2..846467c 100644 --- a/features/step_definitions/steps.rb +++ b/features/step_definitions/steps.rb @@ -96,7 +96,7 @@ end Then(/^Text File "([^"]+)" contains "([^"]+)"$/) do |file, substring| - raise "File #{file} doesn't exit" unless File.exist?(file) + raise "File #{file} doesn't exist" unless File.exist?(file) content = File.read(file) raise "File #{file} doesn't contain \"#{substring}\":\n#{content}" \