Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 19, 2024
2 parents 01f8f13 + 93eb0d3 commit 955d53a
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pdd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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')}' \
Expand Down
79 changes: 79 additions & 0 deletions assets/puzzles_json.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0"?>
<!--
(The MIT License)
Copyright (c) 2014-2024 Yegor Bugayenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/"><xsl:text disable-output-escaping="yes">{</xsl:text><xsl:apply-templates select="puzzles"/>
"puzzles": [
<xsl:apply-templates select="puzzles/puzzle"/>
<xsl:text disable-output-escaping="yes">
]
}</xsl:text>
</xsl:template>
<xsl:template match="puzzles">
<xsl:text>
"version": "</xsl:text>
<xsl:value-of select="@version"/>
<xsl:text>",</xsl:text>
<xsl:text>
"date": "</xsl:text>
<xsl:value-of select="@date"/>
<xsl:text>", </xsl:text>
</xsl:template>
<xsl:template match="puzzle">
<xsl:text disable-output-escaping="no">{</xsl:text>
<xsl:text>
"id": "</xsl:text>
<xsl:value-of select="id"/>
<xsl:text>", </xsl:text>
<xsl:text>
"ticket": "</xsl:text>
<xsl:value-of select="ticket"/>
<xsl:text>", </xsl:text>
<xsl:text>
"file": "</xsl:text>
<xsl:value-of select="file"/>
<xsl:text>", </xsl:text>
<xsl:text>
"lines": "</xsl:text>
<xsl:value-of select="lines"/>
<xsl:text>", </xsl:text>
<xsl:text>
"body": "</xsl:text>
<xsl:value-of select="translate(body, '&quot;', '&#x201C;')"/>
<xsl:text>", </xsl:text>
<xsl:text>
"estimate": "</xsl:text>
<xsl:value-of select="estimate"/>
<xsl:text>", </xsl:text>
<xsl:text>
"role": "</xsl:text>
<xsl:value-of select="role"/>
<xsl:text>"</xsl:text>
<xsl:text disable-output-escaping="yes">
}</xsl:text>
<xsl:if test="position() != last()">,
</xsl:if>
</xsl:template>
</xsl:stylesheet>
13 changes: 10 additions & 3 deletions bin/pdd
Original file line number Diff line number Diff line change
Expand Up @@ -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: ';'
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cucumber.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions features/json_output.feature
Original file line number Diff line number Diff line change
@@ -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 “:)“"
8 changes: 8 additions & 0 deletions features/step_definitions/steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 955d53a

Please sign in to comment.