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 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/assets/puzzles_json.xsl b/assets/puzzles_json.xsl new file mode 100644 index 0000000..4b04a5e --- /dev/null +++ b/assets/puzzles_json.xsl @@ -0,0 +1,79 @@ + + + + + { + "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/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 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..846467c 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 exist" 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