knapsack icon indicating copy to clipboard operation
knapsack copied to clipboard

Multiline declaration for circleci

Open Startouf opened this issue 8 years ago • 4 comments

Hello, I know this is probably not the best place but I couldn't find a link to a discussion in the readme.

I can't get knapsack to work in my cucumber file with parralalism and custom arguments on a multiline yml

test:
  override:
    - mkdir -p $CIRCLE_TEST_REPORTS/cucumber
    # Run critical tests first
    - >
        KNAPSACK_REPORT_PATH=".circleci/knapsack_cucumber_critical_report.json"
        RAILS_ENV=test bundle exec rake "knapsack:cucumber[
          -t @critical
          --format junit --out $CIRCLE_TEST_REPORTS/cucumber/junit.xml
          --format json --out $CIRCLE_TEST_REPORTS/cucumber/tests.cucumber
        ]":
        parallel: true

Gives me errors when parsing on circleci. I've also tried

test:
  override:
    - mkdir -p $CIRCLE_TEST_REPORTS/cucumber
    # Run critical tests first
    - "KNAPSACK_REPORT_PATH=\".circleci/knapsack_cucumber_critical_report.json\"
        RAILS_ENV=test bundle exec rake \"knapsack:cucumber[
          -t @critical
          --format junit --out $CIRCLE_TEST_REPORTS/cucumber/junit.xml
          --format json --out $CIRCLE_TEST_REPORTS/cucumber/tests.cucumber
        ]\"":
        parallel: true

But I run into parsing errors. How did you manage to make multiline declarations with the parallel syntax for circleci ? I saw examples of custom arguments, and how to make the build parallel, but nos how to write that nicely in multiline.

Best error I got was

Don't know how to build task 'knapsack:cucumber[
  -t @critical
  --format junit --out /tmp/circle-junit.syU98Vs/cucumber/junit.xml
  --format json --out /tmp/circle-junit.syU98Vs/cucumber/tests.cucumber
]'

Startouf avatar Apr 05 '17 17:04 Startouf

You can create an executable bash script and run it instead:

You can put this file in your bin directory: bin/knapsack_cucumber

#!/bin/bash

mkdir -p $CIRCLE_TEST_REPORTS/cucumber

KNAPSACK_REPORT_PATH=".circleci/knapsack_cucumber_critical_report.json" \
RAILS_ENV=test \
bundle exec rake "knapsack:cucumber[-t @critical --format junit --out $CIRCLE_TEST_REPORTS/cucumber/junit.xml --format json --out $CIRCLE_TEST_REPORTS/cucumber/tests.cucumber]"

and then

# circle.yml
test:
  override:
    - bin/knapsack_cucumber:
        parallel: true # Caution: there are 8 spaces indentation!

ArturT avatar Apr 05 '17 21:04 ArturT

Hey I've tried your solution and I run into a different error (not sure it's related to the first one) but here is the trick

I use a gem for my payment system (Stripe) and a mock gem for test environment. This gem is declared using a special require in the gemfile

group :test do
  gem 'stripe-ruby-mock', '~> 2.3.1', :require => 'stripe_mock'
end

Now, when I ssh into my circleci container, and I run cucumber, the tests are executing fine. However when I run the tests through the rake task executable nested in the /bin directory as you mentioned, I run into a weird error

cannot load such file -- stripe_mock (LoadError)

Here is the full stack

cannot load such file -- stripe_mock (LoadError)
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `require'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/runtime.rb:91:in `block (2 levels) in require'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `each'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/runtime.rb:86:in `block in require'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `each'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/runtime.rb:75:in `require'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler.rb:106:in `require'
/home/ubuntu/MyApp/config/application.rb:19:in `<top (required)>'
/opt/circleci/ruby/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/opt/circleci/ruby/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/ubuntu/MyApp/config/environment.rb:2:in `<top (required)>'
/opt/circleci/ruby/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/opt/circleci/ruby/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-rails-1.4.5/lib/cucumber/rails.rb:7:in `<top (required)>'
/opt/circleci/ruby/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/opt/circleci/ruby/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/ubuntu/MyApp/features/support/env.rb:10:in `<top (required)>'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/rb_support/rb_language.rb:96:in `load'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/rb_support/rb_language.rb:96:in `load_code_file'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:142:in `load_file'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:84:in `block in load_files!'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:83:in `each'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/runtime/support_code.rb:83:in `load_files!'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/runtime.rb:253:in `load_step_definitions'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/runtime.rb:61:in `run!'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/lib/cucumber/cli/main.rb:32:in `execute!'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/gems/cucumber-2.4.0/bin/cucumber:8:in `<top (required)>'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/bin/cucumber:23:in `load'
/home/ubuntu/MyApp/vendor/bundle/ruby/2.3.0/bin/cucumber:23:in `<top (required)>'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `load'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:74:in `kernel_load'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/cli/exec.rb:27:in `run'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/cli.rb:332:in `exec'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/cli.rb:20:in `dispatch'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/cli.rb:11:in `start'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/exe/bundle:34:in `block in <top (required)>'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
/opt/circleci/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.7/exe/bundle:26:in `<top (required)>'
/opt/circleci/.rvm/gems/ruby-2.3.1/bin/bundle:23:in `load'
/opt/circleci/.rvm/gems/ruby-2.3.1/bin/bundle:23:in `<main>'

And for reference, the relevant lines of my app

# /home/ubuntu/MyApp/config/application.rb:19:in `<top (required)>'
Bundler.require(*Rails.groups)

# /home/ubuntu/MyApp/config/environment.rb:2:in `<top (required)>'
require File.expand_path('../application', __FILE__)

# /home/ubuntu/MyApp/features/support/env.rb:10:in `<top (required)>'
require 'cucumber/rails'

I want to highlight again that everything runs fine when I just run cucumber from the SSH shell of my circleci v1 container, and I tried again forcing the environment to test just to make sure with ENV=test bin/bin/knapsack_cucumber.

EDIT : the whole dependency management seems broken with the rake task. after commentin the require in my gemfile, the crash occurs on

cannot load such file -- cucumber/rails/action_controller (LoadError)

I'm not sure what I am doing wrong...

Startouf avatar Apr 05 '17 22:04 Startouf

How you run cucumber without knapsack? Like that cd project_dir && bundle exec cucumber?

You should run knapsack this way: cd project_dir && ./bin/knapsack_cucumber to ensure you run it from the project_dir and the require paths can be resolved properly.

ArturT avatar Apr 08 '17 17:04 ArturT

Without knapsack and parallelism, the following works

test:
  override:
    - mkdir -p $CIRCLE_TEST_REPORTS/cucumber
    - >
      RAILS_ENV=test bundle exec cucumber
      -t @critical
      --format junit
      --out $CIRCLE_TEST_REPORTS/cucumber/junit.xml

With knapsack I was trying something like

test:
  pre:
  override:
    - mkdir -p $CIRCLE_TEST_REPORTS/cucumber
    - bin/knapsack_cucumber_critical_circleci:
        parallel: true

And the content of the bin/knapsack_cucumber_critical_circleci (which doesn't work, cf error in 3rd comment)

#!/bin/sh
mkdir -p $CIRCLE_TEST_REPORTS/cucumber

KNAPSACK_REPORT_PATH=".circleci/knapsack_cucumber_critical_report.json" \
RAILS_ENV=test \
bundle exec rake "knapsack:cucumber[\
	-t @critical \
	--format junit --out $CIRCLE_TEST_REPORTS/cucumber/junit.xml \
	--format json --out $CIRCLE_TEST_REPORTS/cucumber/ \
]"

Startouf avatar Apr 23 '17 17:04 Startouf

Regarding the stripe error:

cannot load such file -- stripe_mock (LoadError)

You can set the environment variable:

export BUNDLE_DISABLE_EXEC_LOAD=true

It should fix it. More about this problem: https://knapsackpro.com/faq/question/how-to-solve-error-cannot-load-such-file-stripe_mock-loaderror-when-running-tests-with-cucumber

ArturT avatar Oct 28 '22 13:10 ArturT

This issue is about the CircleCI 1.0 YML config syntax. It's deprecated.

The CircleCI 2.0 YML config example can be found here: https://docs.knapsackpro.com/ruby/knapsack#info-for-circleci-users

I'm closing this issue.

ArturT avatar Oct 28 '22 13:10 ArturT