Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ylecuyer committed Dec 1, 2024
0 parents commit 152a725
Show file tree
Hide file tree
Showing 21 changed files with 2,001 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Ruby

on:
push:
branches:
- master

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.3.6'

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Prepare js
run: |
cd js/instrumenter
yarn install --frozen-lockfile
yarn run build
- name: Tests
run: bundle exec rspec
- name: Build release
run: ./scripts/release.sh
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status

**/dist/
4 changes: 4 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--format documentation
--color
--require spec_helper
--order random
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Unreleased
==========

**Key breaking changes:**

New features:

Updated features:

Removed features:

Internal improvements:

Testing improvements:

Others:

v0.1.0
======

* Initial release
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in propshaft-js-coverage.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"
137 changes: 137 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
PATH
remote: .
specs:
propshaft-js-coverage (0.1.0)
mini_racer (~> 0.16.0)
propshaft (~> 1.1.0)

GEM
remote: https://rubygems.org/
specs:
actionpack (7.2.2)
actionview (= 7.2.2)
activesupport (= 7.2.2)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4, < 3.2)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
useragent (~> 0.16)
actionview (7.2.2)
activesupport (= 7.2.2)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activesupport (7.2.2)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.8)
builder (3.3.0)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
diff-lcs (1.5.1)
drb (2.2.1)
erubi (1.13.0)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
libv8-node (18.19.0.0)
libv8-node (18.19.0.0-x86_64-linux)
logger (1.6.1)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mini_portile2 (2.8.8)
mini_racer (0.16.0)
libv8-node (~> 18.19.0.0)
minitest (5.25.2)
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
propshaft (1.1.0)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
rack
railties (>= 7.0.0)
psych (5.2.0)
stringio
racc (1.8.1)
rack (3.1.8)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.2.1)
rack (>= 3)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.2.2)
actionpack (= 7.2.2)
activesupport (= 7.2.2)
irb (~> 1.13)
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rake (13.2.1)
rdoc (6.8.1)
psych (>= 4.0.0)
reline (0.5.12)
io-console (~> 0.5)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
securerandom (0.3.2)
stringio (3.1.2)
thor (1.3.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
useragent (0.16.10)
zeitwerk (2.6.18)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
propshaft-js-coverage!
rake (~> 13.0)
rspec (~> 3.0)

BUNDLED WITH
2.5.9
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Yoann Lecuyer

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.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Installation

Add this line to your application's Gemfile:

```ruby
gem 'propshaft-js-coverage'
```

Add the compiler to propshaft

```ruby
# config/initializers/assets.rb

if ENV["COVERAGE"]
Propshaft::Js::Coverage::Compiler.configure do |config|
config.should_process = ->(path) {
return false if path.match?(/vendor\/assets\//)
return false if path.match?(/gems\//)
return true
}
end

Rails.application.config.assets.compilers << [
"text/javascript", Propshaft::Js::Coverage::Compiler
]
end
```

Get the coverage reports after running your tests

```ruby
# test/application_system_test_case.rb

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
def teardown
__coverage__ = page.evaluate_script <<-JS
JSON.stringify((typeof __coverage__ !== 'undefined') ? __coverage__ : null)
JS

if __coverage__ != "null"
File.write("#{JS_COVERAGE_DIR}/#{Time.now.to_i.to_s}.json", __coverage__)
end

super
end
end
```

Convert to lcov and generate html report

```bash
nyc report --reporter=lcov --temp-dir tmp/js-coverage --report-dir tmp/js-coverage
genhtml -q -o ./coverage ./tmp/js-coverage/lcov.info ./coverage/lcov/simplecov.lcov
```

# How it works

This gem uses istanbul instrumenter to add coverage to your javascript files. It will add a global variable `__coverage__` to your javascript files. You can then use this variable to get the coverage report.

# Configuration

TBD

# Contributing

Clone the repo and run `bundle install` to install the dependencies.

Prepare the js script by running `yarn install` and `yarn run build` in the `js/instrumenter` directory.

# Release

Run `./scripts/release.sh` to release a new version after updating the version in `lib/propshaft/js/coverage/version.rb`.
1 change: 1 addition & 0 deletions js/instrumenter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
22 changes: 22 additions & 0 deletions js/instrumenter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "instrumenter",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "webpack-cli"
},
"dependencies": {
"istanbul-lib-instrument": "^6.0.3"
},
"devDependencies": {
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.1",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"vm-browserify": "^1.1.2",
"webpack": "^5.96.1",
"webpack-cli": "^5.1.4"
}
}
13 changes: 13 additions & 0 deletions js/instrumenter/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var { createInstrumenter } = require('istanbul-lib-instrument')

var instrumenter = createInstrumenter({
esModules: true,
coverageGlobalScopeFunc: false,
coverageGlobalScope: 'window',
})

instrument = function(input, path) {
return instrumenter.instrumentSync(input, path);
}

instrumenterReady = true
22 changes: 22 additions & 0 deletions js/instrumenter/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const webpack = require('webpack');

module.exports = {
mode: 'development',
resolve: {
fallback: {
"path": require.resolve("path-browserify"),
"crypto": require.resolve("crypto-browserify"),
"buffer": require.resolve("buffer/"),
"stream": require.resolve("stream-browserify"),
"vm": require.resolve("vm-browserify"),
"process": require.resolve("process"),
"assert": false,
"fs": false,
}
},
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
}),
],
}
Loading

0 comments on commit 152a725

Please sign in to comment.