Skip to content

Commit

Permalink
Add specs with spec/dummy app (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz authored Apr 30, 2024
1 parent 97d4dca commit d757dbc
Show file tree
Hide file tree
Showing 122 changed files with 3,608 additions and 4,867 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/command_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.0"
ruby-version: "2.7"
bundler-cache: true
- name: Install dependencies
run: bundle install
Expand Down
56 changes: 56 additions & 0 deletions .github/workflows/rspec-shared.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: RSpec Shared

on:
workflow_call:
inputs:
os-version:
required: true
type: string
ruby-version:
required: true
type: string
test-tag:
required: true
type: string

jobs:
rspec:
runs-on: ${{ inputs.os-version }}
env:
RAILS_ENV: test
# We have to add "_CI" to the end, otherwise it messes with tests where we switch profiles,
# as Control Plane will try to use this token's profile instead.
CPLN_TOKEN_CI: ${{ secrets.CPLN_TOKEN }}
CPLN_ORG: ${{ vars.CPLN_ORG }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ inputs.ruby-version }}
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Install Control Plane tools
run: |
sudo npm install -g @controlplane/cli
cpln --version
- name: Setup Control Plane tools
run: |
cpln profile create default --token $CPLN_TOKEN_CI --org $CPLN_ORG
cpln image docker-login
- name: Run tests
run: bundle exec rspec --format documentation --tag ${{ inputs.test-tag }}
- name: Upload spec log
uses: actions/upload-artifact@master
if: always()
with:
name: spec-${{ inputs.test-tag }}-${{ github.run_id }}-${{ inputs.os-version }}-${{ inputs.ruby-version }}.log
path: spec.log
- name: Upload coverage results
uses: actions/upload-artifact@master
if: always()
with:
name: coverage-report-${{ inputs.test-tag }}-${{ github.run_id }}-${{ inputs.os-version }}-${{ inputs.ruby-version }}
path: coverage
50 changes: 19 additions & 31 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,24 @@ on:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
rspec:
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
ruby:
- "2.7"
- "3.0"
runs-on: ${{ matrix.os }}
name: RSpec
env:
RAILS_ENV: test
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Run tests
run: bundle exec rspec
- name: Upload coverage results
uses: actions/upload-artifact@master
if: always()
with:
name: coverage-report-${{ github.run_id }}-${{ matrix.os }}-${{ matrix.ruby }}
path: coverage
rspec-fast:
name: RSpec (Fast)
uses: ./.github/workflows/rspec-shared.yml
with:
os-version: ubuntu-latest
ruby-version: "2.7"
test-tag: ~slow
secrets: inherit

rspec-slow:
name: RSpec (Slow)
uses: ./.github/workflows/rspec-shared.yml
if: github.event_name == 'workflow_dispatch'
with:
os-version: ubuntu-latest
ruby-version: "2.7"
test-tag: slow
secrets: inherit
12 changes: 2 additions & 10 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,15 @@ on:

jobs:
rubocop:
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
ruby:
- "2.7"
- "3.0"
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
name: Rubocop
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
ruby-version: "2.7"
bundler-cache: true
- name: Install dependencies
run: bundle install
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# rspec failure tracking
.rspec_status

spec.log
1 change: 0 additions & 1 deletion .rspec

This file was deleted.

10 changes: 10 additions & 0 deletions .simplecov_spawn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "simplecov"

SimpleCov.start do
command_name "spawn"
enable_coverage :branch

at_fork.call(Process.pid)
end
27 changes: 25 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ git clone https://github.com/shakacode/heroku-to-control-plane
alias cpl="~/projects/heroku-to-control-plane/bin/cpl"
```

## Linting/Testing
## Linting

Before committing or pushing code, be sure to:

- Run `bundle exec rake update_command_docs` to sync any doc changes made in the source code to the docs
- Run `bundle exec rubocop -a` to fix any linting errors
- Run `bundle exec rspec` to run the test suite

You can also install [overcommit](https://github.com/sds/overcommit) and let it automatically check for you:

Expand All @@ -28,6 +27,30 @@ gem install overcommit
overcommit --install
```

## Testing

We use real apps for the tests. You'll need to have full access to a Control Plane org, and then set it as the env var `CPLN_ORG` when running the tests (or in the `.env` file):

```sh
CPLN_ORG=your-org-for-tests bundle exec rspec
```

Tests are separated between fast and slow. Slow tests can take a long time and usually involve building / deploying images and waiting for workloads to be ready / not ready, so they should only be run once in a while.

If you add a slow test, tag it with `slow`. Tests without a `slow` tag are considered fast by default.

To run fast tests:

```sh
CPLN_ORG=your-org-for-tests bundle exec rspec --tag ~slow
```

To run slow tests:

```sh
CPLN_ORG=your-org-for-tests bundle exec rspec --tag slow
```

## Debugging

1. Use the `--verbose` option to see more detailed logs.
Expand Down
57 changes: 31 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,66 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.4)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.7)
childprocess (4.1.0)
crack (0.4.5)
crack (1.0.0)
bigdecimal
rexml
debug (1.7.2)
irb (>= 1.5.0)
reline (>= 0.3.1)
diff-lcs (1.5.0)
diff-lcs (1.5.1)
docile (1.4.0)
dotenv (2.8.1)
hashdiff (1.0.1)
hashdiff (1.1.0)
iniparse (1.5.0)
io-console (0.7.2)
irb (1.12.0)
rdoc
reline (>= 0.4.2)
json (2.6.3)
json (2.7.2)
jwt (2.8.1)
base64
overcommit (0.60.0)
childprocess (>= 0.6.3, < 5)
iniparse (~> 1.4)
rexml (~> 3.2)
parallel (1.22.1)
parser (3.2.0.0)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
psych (5.1.2)
stringio
public_suffix (5.0.1)
public_suffix (5.0.5)
racc (1.7.3)
rainbow (3.1.1)
rake (13.0.6)
rdoc (6.6.2)
rake (13.2.1)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.6.2)
reline (0.4.3)
regexp_parser (2.9.0)
reline (0.5.1)
io-console (~> 0.5)
rexml (3.2.5)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.1)
rspec-core (3.12.3)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
rspec-expectations (3.12.4)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.3)
rspec-mocks (3.12.7)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.45.0)
rspec-retry (0.6.2)
rspec-core (> 3.3)
rspec-support (3.12.2)
rubocop (1.45.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
Expand All @@ -74,16 +80,16 @@ GEM
rubocop-ast (>= 1.24.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.24.1)
parser (>= 3.1.1.0)
rubocop-capybara (2.17.0)
rubocop-ast (1.31.2)
parser (>= 3.3.0.4)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.18.1)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
ruby-progressbar (1.11.0)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -92,9 +98,8 @@ GEM
simplecov_json_formatter (0.1.4)
stringio (3.1.0)
thor (1.2.2)
timecop (0.9.6)
unicode-display_width (2.4.2)
vcr (6.1.0)
timecop (0.9.8)
unicode-display_width (2.5.0)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
Expand All @@ -109,12 +114,12 @@ DEPENDENCIES
overcommit (~> 0.60.0)
rake (~> 13.0)
rspec (~> 3.12.0)
rspec-retry (~> 0.6.2)
rubocop (~> 1.45.0)
rubocop-rake (~> 0.6.0)
rubocop-rspec (~> 2.18.1)
simplecov (~> 0.22.0)
timecop (~> 0.9.6)
vcr (~> 6.1.0)
webmock (~> 3.18.1)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion cpl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "overcommit", "~> 0.60.0"
spec.add_development_dependency "rspec", "~> 3.12.0"
spec.add_development_dependency "rspec-retry", "~> 0.6.2"
spec.add_development_dependency "rubocop", "~> 1.45.0"
spec.add_development_dependency "rubocop-rake", "~> 0.6.0"
spec.add_development_dependency "rubocop-rspec", "~> 2.18.1"
spec.add_development_dependency "simplecov", "~> 0.22.0"
spec.add_development_dependency "timecop", "~> 0.9.6"
spec.add_development_dependency "vcr", "~> 6.1.0"
spec.add_development_dependency "webmock", "~> 3.18.1"

spec.files = `git ls-files -z`.split("\x0").reject do |file|
Expand Down
14 changes: 7 additions & 7 deletions lib/command/apply_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def call # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, M
pending_templates.each do |template, filename|
step("Applying template '#{template}'", abort_on_error: false) do
items = apply_template(filename)
if items
items.each do |item|
report_success(item)
end
else
unless items
report_failure(template)
next false
end

$CHILD_STATUS.success?
items.each do |item|
report_success(item)
end
true
end
end

Expand All @@ -82,7 +82,7 @@ def call # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, M
print_failed_templates
print_skipped_templates

exit(1) if @failed_templates.any?
exit(ExitCode::ERROR_DEFAULT) if @failed_templates.any?
end

private
Expand Down
Loading

0 comments on commit d757dbc

Please sign in to comment.