diff --git a/.github/workflows/assets.yml b/.github/workflows/assets.yml new file mode 100644 index 00000000..d38e7e59 --- /dev/null +++ b/.github/workflows/assets.yml @@ -0,0 +1,32 @@ +name: Assets + +on: + pull_request: + push: + branches: + - main + schedule: + - cron: '0 0 * * 0' # weekly + +jobs: + assets: + timeout-minutes: 30 + runs-on: ubuntu-18.04 + + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-ruby@v1 + with: + ruby-version: 2.6 + - uses: actions/cache@v2 + with: + path: vendor/bundle + key: ${{ runner.os }}-gems-pr-${{ hashFiles('**/Gemfile.lock') }} + restore-keys: | + ${{ runner.os }}-gems-pr- + - name: Build assets + run: | + gem install bundler + bundle config path vendor/bundle + bundle install --jobs 4 --retry 3 + bundle exec rake assets:precompile diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3e659ee1..64ec0589 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,15 +20,7 @@ jobs: options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 env: POSTGRES_USER: postgres - POSTGRES_DB: ci_test POSTGRES_PASSWORD: postgres - strategy: - matrix: - tests: - - name: 'assets' - args: 'ASSETS=true' - - name: 'tests' - args: 'ASSETS=false' steps: - uses: actions/checkout@v2 @@ -44,12 +36,12 @@ jobs: - name: Test env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - OXT_DB_USER: postgres - OXT_TEST_DB: ci_test - OXT_DB_PASS: postgres + OXE_DB_USER: postgres + OXE_DB_PASS: postgres RAILS_ENV: test run: | gem install bundler bundle config path vendor/bundle bundle install --jobs 4 --retry 3 - ${{ matrix.tests.args }} ./bin/ci + bundle exec rake parallel:create parallel:load_schema parallel:seed --trace + bundle exec parallel_rspec ./spec diff --git a/app/routines/attach_file.rb b/app/routines/attach_file.rb deleted file mode 100644 index cb5b33b9..00000000 --- a/app/routines/attach_file.rb +++ /dev/null @@ -1,45 +0,0 @@ -class AttachFile - - lev_routine - - protected - - def exec(attachable:, file: nil, url: nil) - if file.present? - file = File.open(file) - unlink_file = false - elsif url.present? - contents = Net::HTTP.get(URI.parse(url)) - file = Tempfile.new('attachment') - file.binmode - file.write(contents) - file.rewind - unlink_file = true - else - fatal_error(code: :no_path_or_url, message: 'You must specify either a local file or a url') - end - - attachment = Attachment.new(parent: attachable, asset: file) - existing_attachment = attachable.attachments.find { |att| att.filename == attachment.filename } - if existing_attachment.nil? - # The attachment MUST be saved or else the URL's returned will be wrong (tmp folder) - attachment.save! - attachable.attachments.reset - transfer_errors_from(attachment, {type: :verbatim}, true) - Rails.logger.info "New attachment: #{attachment.asset.url}" - else - attachment = existing_attachment - Rails.logger.info "Reused attachment: #{attachment.asset.url}" - end - - unlink_file ? file.close! : file.close - - outputs.attachment = attachment - outputs.url = attachment.asset.url - return unless attachment.asset.is_image? - - outputs.large_url = attachment.asset.large.url - outputs.medium_url = attachment.asset.medium.url - outputs.small_url = attachment.asset.small.url - end -end diff --git a/bin/ci b/bin/ci deleted file mode 100755 index 6e6c99cc..00000000 --- a/bin/ci +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -set -eo pipefail - -if [[ -n "${ASSETS}" ]]; then - bundle exec rake assets:precompile -elif [[ -n "${TAG}" ]]; then - bundle exec rake parallel:create parallel:load_schema parallel:seed --trace - bundle exec parallel_rspec ./spec --test-options "--tag $TAG" -else - echo Must be called with ENV of ASSETS or TAG set - exit 1 -fi diff --git a/spec/representers/api/v1/exercises/representer_spec.rb b/spec/representers/api/v1/exercises/representer_spec.rb index 8f3c821e..e64da0ed 100644 --- a/spec/representers/api/v1/exercises/representer_spec.rb +++ b/spec/representers/api/v1/exercises/representer_spec.rb @@ -110,17 +110,16 @@ module Api::V1::Exercises end it 'only attaches when not already attached' do + # attempt to attach an image that is already attached + representer = described_class.new(exercise) expect { - # attempt to attach an image that is already attached - representer = described_class.new(exercise) - expect { - representer.from_hash('images' => [ - { 'signed_id' => exercise.images[0].signed_id }, - { 'signed_id' => exercise.images[0].signed_id }, - ] - ) - exercise.save - }.not_to change { exercise.images.count } + representer.from_hash('images' => [ + { 'signed_id' => exercise.images[0].signed_id }, + { 'signed_id' => exercise.images[0].signed_id }, + ] + ) + exercise.save + }.not_to change { exercise.images.count } end end diff --git a/spec/routines/attach_file_spec.rb b/spec/routines/attach_file_spec.rb deleted file mode 100644 index d635db2f..00000000 --- a/spec/routines/attach_file_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -require "rails_helper" - -RSpec.describe AttachFile, type: :routine do - let(:attachable) { FactoryBot.build :exercise } - - it 'attaches the file in the given path to the given attachable object' do - result = nil - expect do - result = described_class.call(attachable: attachable, - file: 'spec/fixtures/os_exercises_logo.png') - end.to change { attachable.attachments.size }.by(1) - expect(result.errors).to be_empty - expect(attachable).to be_persisted - - expect(attachable.attachments.order(:created_at).last.asset.url).to eq result.outputs.url - end - - it 'reuses existing attachments' do - result = nil - expect{ - result = described_class.call(attachable: attachable, - file: 'spec/fixtures/os_exercises_logo.png') - }.to change{ attachable.attachments.size }.by(1) - expect(result.errors).to be_empty - expect(attachable).to be_persisted - - expect{ - result = described_class.call(attachable: attachable, - file: 'spec/fixtures/os_exercises_logo.png') - }.not_to change{ attachable.attachments.size } - expect(result.errors).to be_empty - end - - it 'sets the attachment and url for different sizes as outputs' do - output = described_class.call(attachable: attachable, - file: 'spec/fixtures/os_exercises_logo.png').outputs - expect(attachable).to be_persisted - attachment = attachable.attachments.order(:created_at).last - expect(output.as_json).to match( - 'attachment' => a_hash_including( - "id" => attachment.id, - "parent_id" => attachment.parent.id, - "parent_type" => "Exercise" - ), - 'large_url' => a_string_starting_with( - "https://not-a-real-bucket.s3.amazonaws.com/test/large_" - ), - 'medium_url' => a_string_starting_with( - "https://not-a-real-bucket.s3.amazonaws.com/test/medium_" - ), - 'small_url' => a_string_starting_with( - "https://not-a-real-bucket.s3.amazonaws.com/test/small_" - ), - 'url' => a_string_starting_with("https://not-a-real-bucket.s3.amazonaws.com/test/") - ) - end -end