<commit_before>require_relative '../../../spec_helper'

describe Dapp::Dimg::Config::Directive::Artifact do
  include SpecHelper::Common
  include SpecHelper::Config::Dimg

  def dappfile_dimg_with_artifact(&blk)
    dappfile do
      dimg_group do
        artifact do
          instance_eval(&blk)
        end

        dimg 'name'
      end
    end
  end

  context 'positive' do
    it 'shell.build_artifact' do
      dappfile_dimg_with_artifact do
        shell do
          build_artifact do
            run 'cmd'
          end
        end
        export '/cwd'
      end

      expect(dimg_config._artifact.first._config._shell._build_artifact_command).to eq ['cmd']
    end

    [:before, :after].each do |order|
      [:setup, :install].each do |stage|
        it "#{order}_#{stage}_artifact" do
          dappfile_dimg_with_artifact do
            export '/cwd' do
              send(order, stage)
            end
          end

          expect(dimg_config.public_send(:"_#{order}_#{stage}_artifact").first).to_not be_nil
        end
      end
    end

    it 'default cwd (to)' do
      dappfile_dimg_with_artifact do
        export do
          before :setup
          to '/a'
        end
      end

      expect(dimg_config._before_setup_artifact.first._cwd).to eq '/a'
    end
  end

  context 'negative' do
    [:volume, :expose, :env, :label, :cmd, :onbuild, :workdir, :user, :entrypoint].each do |instruction|
      it "unsupported docker.#{instruction} in artifact body" do
        dappfile_dimg_with_artifact do
          docker do
            send(instruction, 'value')
          end

          export do
            to '/to'
          end
        end

        expect { dimgs_configs }.to raise_error NoMethodError
      end
    end

    it 'double associated (:stage_artifact_double_associate)' do
      dappfile_dimg_with_artifact do
        export '/cwd' do
          before :setup
          after :setup
        end
      end

      expect_exception_code(:stage_artifact_double_associate) { dimgs_configs }
    end

    it 'not supported associated stage (:stage_artifact_not_supported_associated_stage)' do
      dappfile_dimg_with_artifact do
        export '/cwd' do
          before :from
        end
      end

      expect_exception_code(:stage_artifact_not_supported_associated_stage) { dimgs_configs }<commit_msg>modify artifact_spec<commit_after>require_relative '../../../spec_helper'

describe Dapp::Dimg::Config::Directive::Artifact do
  include SpecHelper::Common
  include SpecHelper::Config::Dimg

  def dappfile_dimg_with_artifact(&blk)
    dappfile do
      dimg_group do
        artifact do
          instance_eval(&blk)
        end

        dimg 'name'
      end
    end
  end

  context 'positive' do
    it 'shell.build_artifact' do
      dappfile_dimg_with_artifact do
        shell do
          build_artifact do
            run 'cmd'
          end
        end
        export '/cwd'
      end

      expect(dimg_config._artifact.first._config._shell._build_artifact_command).to eq ['cmd']
    end

    [:before, :after].each do |order|
      [:setup, :install].each do |stage|
        it "#{order}_#{stage}_artifact" do
          dappfile_dimg_with_artifact do
            export '/cwd' do
              send(order, stage)
            end
          end

          expect(dimg_config.public_send(:"_#{order}_#{stage}_artifact").first).to_not be_nil
        end
      end
    end

    it 'default cwd (to)' do
      dappfile_dimg_with_artifact do
        export do
          before :setup
          to '/a'
        end
      end

      expect(dimg_config._before_setup_artifact.first._cwd).to eq '/a'
    end
  end

  context 'negative' do
    [:volume, :expose, :env, :label, :cmd, :onbuild, :workdir, :user, :entrypoint].each do |instruction|
      it "unsupported docker.#{instruction} in artifact body" do
        dappfile_dimg_with_artifact do
          docker do
            send(instruction, 'value')
          end

          export do
            to '/to'
          end
        end

        expect_exception_code(:docker_artifact_unsupported_directive) { dimgs_configs }
      end
    end

    it 'double associated (:stage_artifact_double_associate)' do
      dappfile_dimg_with_artifact do
        export '/cwd' do
          before :setup
          after :setup
        end
      end

      expect_exception_code(:stage_artifact_double_associate) { dimgs_configs }
    end

    it 'not supported associated stage (:stage_artifact_not_supported_associated_stage)' do
      dappfile_dimg_with_artifact do
        export '/cwd' do
          before :from
        end
      end

      expect_exception_code(:stage_artifact_not_supported_associated_stage) { dimgs_configs }
{'fraction_non_alphanumeric': 0.08133869942808727, 'fraction_numerical': 0.0, 'mean_word_length': 2.112722478576137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '235547', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1395, 'n_words': 338}