simplecov_html_inline 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d4f0ec856cffec37d6772b8f4c428c7f20f8001bcf7f064c0413f34fef887ebe
4
+ data.tar.gz: 88ec81a6728eb27b9985c1e6a5bbe72f6060a31ffbf0a674ddbe75927884b6a3
5
+ SHA512:
6
+ metadata.gz: 6c9c628692d7b37dad9648075621fe6ac57734497e3d569ee1b35ac6bbb56b759b988b49bb1ff1d95f842fbe5ea432274187c7d863dd8be3c689f605b362c4ba
7
+ data.tar.gz: 46ab7630741a579b5c3c0ffaf9cf103c2cd0b7ef1c6fbb1cca7e85c1c0940aed6b54759b9068faa9c900d1da4170d2085c79bca7c2161313704d6432f5f4490d
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ /.bundle
2
+ /.byebug_history
3
+ /Gemfile.lock
4
+ /coverage
5
+ /spec/tmp
6
+
7
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'byebug'
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2021 Toby Hsieh
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # simplecov\_html\_inline
2
+
3
+ This is a Ruby gem that contains `SimplecovHtmlInline::Formatter`, a [SimpleCov](https://github.com/simplecov-ruby/simplecov) formatter based on [simplecov-html](https://github.com/simplecov-ruby/simplecov-html) that uses inline/embedded assets (via data URIs).
4
+
5
+ This is useful for cases such as using continuous integration software that publishes artifacts to a private S3 bucket and lets you view artifacts by responding with a redirect to a signed S3 URL. With simplecov-html, the index.html file won't work because it uses relative URLs for assets, which do not have S3 signatures.
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ require 'simplecov'
11
+ require 'simplecov_html_inline'
12
+
13
+ SimpleCov.start do
14
+ formatter SimplecovHtmlInline::Formatter
15
+ end
16
+ ```
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,2 @@
1
+ require 'simplecov_html_inline/formatter'
2
+ require 'simplecov_html_inline/version'
@@ -0,0 +1,66 @@
1
+ require 'base64'
2
+
3
+ require 'simplecov'
4
+ require 'simplecov-html'
5
+
6
+ class SimplecovHtmlInline
7
+ # A SimpleCov formatter based on {SimpleCov::Formatter::HTMLFormatter} that
8
+ # uses inline/embedded assets
9
+ class Formatter < SimpleCov::Formatter::HTMLFormatter
10
+ # Mapping from file extension to media type
11
+ MEDIA_TYPE_MAPPING = {
12
+ '.css' => 'text/css',
13
+ '.gif' => 'image/gif',
14
+ '.js' => 'text/javascript',
15
+ '.png' => 'image/png',
16
+ }.freeze
17
+
18
+ # Directory where simplecov-html assets live
19
+ PUBLIC_DIR = Gem.find_latest_files(
20
+ 'simplecov-html.rb'
21
+ ).first.then do |simplecov_html_rb_path|
22
+ dir = File.join(File.dirname(simplecov_html_rb_path), '..', 'public')
23
+ File.absolute_path(dir).freeze
24
+ end
25
+
26
+ # Creates an HTML coverage report with inline assets
27
+ #
28
+ # @param result [SimpleCov::Result] the SimpleCov result to format
29
+ def format(result)
30
+ # Same as SimpleCov::Formatter::HTMLFormatter#format except we omit the
31
+ # copying of asset files
32
+ File.open(File.join(output_path, 'index.html'), 'wb') do |file|
33
+ file.puts template('layout').result(binding)
34
+ end
35
+ puts output_message(result)
36
+ end
37
+
38
+ private
39
+
40
+ # Constructs a data URI for an asset with the given name.
41
+ #
42
+ # @note
43
+ # This overrides {SimpleCov::Formatter::HTMLFormatter#assets_path} (which
44
+ # returns a relative URL)
45
+ #
46
+ # @param name [String]
47
+ # name of file (relative to simplecov-html's public directory) to return
48
+ # a data URI for
49
+ # @return [String]
50
+ # data URI for the asset, or an empty string if an asset with the given
51
+ # name does not exist
52
+ def assets_path(name)
53
+ file_path = File.join(PUBLIC_DIR, name)
54
+ return '' unless File.exist?(file_path)
55
+
56
+ media_type = MEDIA_TYPE_MAPPING.fetch(File.extname(name))
57
+ contents = IO.read(file_path)
58
+
59
+ if media_type == 'text/css'
60
+ contents.gsub!(/url\(['"]?(.+?)['"]?\)/) { "url(#{assets_path($1)})" }
61
+ end
62
+
63
+ "data:#{media_type};base64,#{Base64.strict_encode64(contents)}"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ class SimplecovHtmlInline
2
+ VERSION = IO.read(File.join(__dir__, '..', '..', 'VERSION')).chomp
3
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'simplecov_html_inline'
3
+ s.version = IO.read('VERSION').chomp
4
+ s.authors = ['Toby Hsieh']
5
+ s.homepage = 'https://github.com/tobyhs/simplecov_html_inline'
6
+ s.summary = 'SimpleCov HTML formatter with inline assets'
7
+ s.description = 'Formatter based on simplecov-html that uses inline/embedded assets'
8
+ s.license = 'MIT'
9
+
10
+ s.add_dependency 'simplecov', '~> 0.21'
11
+
12
+ s.add_development_dependency 'rspec', '~> 3.10'
13
+ s.add_development_dependency 'selenium-webdriver', '~> 3.142'
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ end
@@ -0,0 +1,5 @@
1
+ module Test1
2
+ def self.testing
3
+ puts 'testing'
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Test2
2
+ def self.abs(number)
3
+ if number >= 0
4
+ number
5
+ else
6
+ -number
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,86 @@
1
+ require 'selenium-webdriver'
2
+
3
+ require 'simplecov_html_inline/formatter'
4
+
5
+ RSpec.describe SimplecovHtmlInline::Formatter do
6
+ let(:coverage_path) { 'spec/tmp/coverage' }
7
+ subject(:formatter) { described_class.new }
8
+
9
+ describe '#format' do
10
+ let(:original_result) do
11
+ {
12
+ 'spec/fixtures/test1.rb' => { 'lines' => [1, 1, 1, nil, nil] },
13
+ 'spec/fixtures/test2.rb' => {
14
+ 'lines' => [1, 1, 1, 1, nil, 0, nil, nil, nil],
15
+ },
16
+ }.transform_keys(&File.method(:absolute_path))
17
+ end
18
+
19
+ let(:result) do
20
+ SimpleCov::Result.new(
21
+ original_result,
22
+ command_name: 'TestTest',
23
+ created_at: Time.at(1615761100)
24
+ )
25
+ end
26
+
27
+ let(:web_driver) { Selenium::WebDriver.for(:chrome) }
28
+
29
+ before do
30
+ FileUtils.rm_rf(coverage_path)
31
+ FileUtils.mkdir_p(coverage_path)
32
+ allow(SimpleCov).to receive(:coverage_path).and_return(coverage_path)
33
+ # spec_helper.rb adds a filter that only includes lib/ files, so we need
34
+ # to stub this here so spec/fixtures/ files are included
35
+ allow(SimpleCov).to receive(:filters).and_return([])
36
+ end
37
+
38
+ after do
39
+ web_driver.quit
40
+ end
41
+
42
+ it 'creates an HTML coverage report with inline assets' do
43
+ formatter.format(result)
44
+
45
+ index_path = File.join(Dir.pwd, coverage_path, 'index.html')
46
+ web_driver.get("file://#{index_path}")
47
+
48
+ aggregate_failures do
49
+ # Check that the JavaScript file is inline
50
+ script_element = web_driver.find_element(:tag_name, 'script')
51
+ expect(script_element.attribute('src')).to start_with(
52
+ 'data:text/javascript;base64,'
53
+ )
54
+
55
+ # Check that the CSS file is inline
56
+ css_element = web_driver.find_element(css: 'link[rel="stylesheet"]')
57
+ css_data_uri = css_element.attribute('href')
58
+ expect(css_data_uri).to start_with('data:text/css;base64,')
59
+
60
+ # Check that the image URLs in the CSS file are data URIs
61
+ css_content = css_data_uri.split(',', 2)[1]
62
+ css_content = Base64.strict_decode64(css_content)
63
+ css_content.scan(/url\((.+?)\)/) do |match|
64
+ expect(match[0]).to match(%r{\Adata:image/[a-z]+;base64,})
65
+ end
66
+
67
+ # Check that clicking a file shows its source
68
+ file_name = 'spec/fixtures/test1.rb'
69
+ file_a_element = web_driver.find_element(css: "a[title='#{file_name}']")
70
+ file_a_element.click
71
+
72
+ file_id = file_a_element.attribute('href').split('#').last
73
+ source_table = web_driver.find_element(id: file_id)
74
+ expect(source_table).to be_displayed
75
+
76
+ web_driver.find_element(id: 'cboxClose').click
77
+ Selenium::WebDriver::Wait.new(
78
+ message: 'Source table for file did not close',
79
+ timeout: 1,
80
+ ).until do
81
+ !source_table.displayed?
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,7 @@
1
+ require 'simplecov_html_inline/version'
2
+
3
+ RSpec.describe 'SimplecovHtmlInline::VERSION' do
4
+ it 'resembles a version string' do
5
+ expect(SimplecovHtmlInline::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start do
4
+ add_filter { |src_file| !src_file.project_filename.start_with?('/lib/') }
5
+
6
+ # Using this odd technique so 'simplecov_html_inline' can be required after
7
+ # SimpleCov starts coverage
8
+ formatter_wrapper = Object.new
9
+ def formatter_wrapper.new
10
+ SimplecovHtmlInline::Formatter.new
11
+ end
12
+ formatter formatter_wrapper
13
+ end
14
+
15
+ require 'simplecov_html_inline'
16
+
17
+ RSpec.configure do |config|
18
+ config.expect_with :rspec do |expectations|
19
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
20
+ end
21
+
22
+ config.mock_with :rspec do |mocks|
23
+ mocks.verify_partial_doubles = true
24
+ end
25
+
26
+ config.shared_context_metadata_behavior = :apply_to_host_groups
27
+ config.filter_run_when_matching :focus
28
+ config.disable_monkey_patching!
29
+ config.warnings = true
30
+
31
+ if config.files_to_run.one?
32
+ config.default_formatter = 'doc'
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplecov_html_inline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Toby Hsieh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simplecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.21'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.21'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: selenium-webdriver
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.142'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.142'
55
+ description: Formatter based on simplecov-html that uses inline/embedded assets
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - ".rspec"
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - VERSION
67
+ - lib/simplecov_html_inline.rb
68
+ - lib/simplecov_html_inline/formatter.rb
69
+ - lib/simplecov_html_inline/version.rb
70
+ - simplecov_html_inline.gemspec
71
+ - spec/fixtures/test1.rb
72
+ - spec/fixtures/test2.rb
73
+ - spec/simplecov_html_inline/formatter_spec.rb
74
+ - spec/simplecov_html_inline/version_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/tobyhs/simplecov_html_inline
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.1.4
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: SimpleCov HTML formatter with inline assets
99
+ test_files: []