|  | 
| 2 | 2 | 
 | 
| 3 | 3 | describe Fastlane::Actions::BuildkiteUploadPipelineAction do | 
| 4 | 4 |   let(:pipeline_file) { 'path/to/pipeline.yml' } | 
| 5 |  | -  let(:branch) { 'feature-branch' } | 
| 6 |  | -  let(:commit) { 'abc123' } | 
| 7 |  | -  let(:commit_default) { Fastlane::Actions::BuildkiteUploadPipelineAction::DEFAULT_COMMIT } | 
| 8 | 5 |   let(:env_file) { 'path/to/env_file' } | 
| 9 | 6 |   let(:env_file_default) { Fastlane::Actions::BuildkiteUploadPipelineAction::DEFAULT_ENV_FILE } | 
|  | 7 | +  let(:environment) { { 'AKEY' => 'AVALUE' } } | 
|  | 8 | +  let(:environment_default) { {} } | 
| 10 | 9 | 
 | 
| 11 | 10 |   before do | 
| 12 | 11 |     allow(File).to receive(:exist?).with(anything) | 
|  | 
| 17 | 16 |   describe 'parameter validation' do | 
| 18 | 17 |     it 'raises an error when pipeline_file is not provided' do | 
| 19 | 18 |       expect do | 
| 20 |  | -        run_described_fastlane_action(branch: branch) | 
|  | 19 | +        run_described_fastlane_action( | 
|  | 20 | +          environment: environment | 
|  | 21 | +        ) | 
| 21 | 22 |       end.to raise_error(FastlaneCore::Interface::FastlaneError, /pipeline_file/) | 
| 22 | 23 |     end | 
| 23 | 24 | 
 | 
| 24 | 25 |     it 'raises an error when pipeline_file does not exist' do | 
| 25 | 26 |       allow(File).to receive(:exist?).with(pipeline_file).and_return(false) | 
| 26 | 27 |       expect do | 
| 27 |  | -        run_described_fastlane_action( | 
| 28 |  | -          pipeline_file: pipeline_file, | 
| 29 |  | -          branch: branch | 
| 30 |  | -        ) | 
|  | 28 | +        run_described_fastlane_action(pipeline_file: pipeline_file) | 
| 31 | 29 |       end.to raise_error(FastlaneCore::Interface::FastlaneError, /Pipeline file not found/) | 
| 32 | 30 |     end | 
| 33 | 31 | 
 | 
| 34 |  | -    it 'raises an error when both branch and commit are provided' do | 
|  | 32 | +    it 'raises an error when not running on Buildkite' do | 
| 35 | 33 |       allow(File).to receive(:exist?).with(pipeline_file).and_return(true) | 
|  | 34 | +      allow(ENV).to receive(:[]).with('BUILDKITE').and_return(nil) | 
|  | 35 | + | 
| 36 | 36 |       expect do | 
| 37 |  | -        run_described_fastlane_action( | 
| 38 |  | -          pipeline_file: pipeline_file, | 
| 39 |  | -          branch: branch, | 
| 40 |  | -          commit: commit | 
| 41 |  | -        ) | 
| 42 |  | -      end.to raise_error(FastlaneCore::Interface::FastlaneError, /You should not provide both `branch` and `commit`/) | 
|  | 37 | +        run_described_fastlane_action(pipeline_file: pipeline_file) | 
|  | 38 | +      end.to raise_error(FastlaneCore::Interface::FastlaneError, /This action can only be called from a Buildkite CI build/) | 
| 43 | 39 |     end | 
| 44 | 40 | 
 | 
| 45 |  | -    it 'uses the default value for commit when not provided' do | 
|  | 41 | +    it 'passes the environment hash to the shell command' do | 
| 46 | 42 |       allow(File).to receive(:exist?).with(pipeline_file).and_return(true) | 
| 47 | 43 |       expect(Fastlane::Action).to receive(:sh).with( | 
| 48 |  | -        { 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default }, | 
|  | 44 | +        environment, | 
| 49 | 45 |         'buildkite-agent', 'pipeline', 'upload', pipeline_file | 
| 50 | 46 |       ) | 
| 51 | 47 |       expect_upload_pipeline_message | 
| 52 | 48 | 
 | 
| 53 | 49 |       run_described_fastlane_action( | 
| 54 | 50 |         pipeline_file: pipeline_file, | 
| 55 |  | -        branch: branch | 
|  | 51 | +        environment: environment | 
| 56 | 52 |       ) | 
| 57 | 53 |     end | 
| 58 | 54 | 
 | 
| 59 |  | -    it 'uses the provided value for the commit' do | 
|  | 55 | +    it 'passes the environment hash to the shell command also with an env_file' do | 
| 60 | 56 |       allow(File).to receive(:exist?).with(pipeline_file).and_return(true) | 
|  | 57 | +      allow(File).to receive(:exist?).with(env_file).and_return(true) | 
| 61 | 58 |       expect(Fastlane::Action).to receive(:sh).with( | 
| 62 |  | -        { 'BUILDKITE_COMMIT' => commit }, | 
| 63 |  | -        'buildkite-agent', 'pipeline', 'upload', pipeline_file | 
|  | 59 | +        environment, | 
|  | 60 | +        "source #{env_file.shellescape} && buildkite-agent pipeline upload #{pipeline_file.shellescape}" | 
| 64 | 61 |       ) | 
| 65 |  | -      expect_upload_pipeline_message(expected_branch: nil, expected_commit: commit) | 
|  | 62 | +      expect_upload_pipeline_message | 
|  | 63 | +      expect_sourcing_env_file_message(env_file) | 
| 66 | 64 | 
 | 
| 67 | 65 |       run_described_fastlane_action( | 
| 68 | 66 |         pipeline_file: pipeline_file, | 
| 69 |  | -        commit: commit | 
|  | 67 | +        environment: environment, | 
|  | 68 | +        env_file: env_file | 
| 70 | 69 |       ) | 
| 71 | 70 |     end | 
| 72 |  | - | 
| 73 |  | -    it 'raises an error when not running on Buildkite' do | 
| 74 |  | -      allow(File).to receive(:exist?).with(pipeline_file).and_return(true) | 
| 75 |  | -      allow(ENV).to receive(:[]).with('BUILDKITE').and_return(nil) | 
| 76 |  | - | 
| 77 |  | -      expect do | 
| 78 |  | -        run_described_fastlane_action( | 
| 79 |  | -          pipeline_file: pipeline_file, | 
| 80 |  | -          branch: branch | 
| 81 |  | -        ) | 
| 82 |  | -      end.to raise_error(FastlaneCore::Interface::FastlaneError, /This action can only be called from a Buildkite CI build/) | 
| 83 |  | -    end | 
| 84 | 71 |   end | 
| 85 | 72 | 
 | 
| 86 | 73 |   describe 'pipeline upload' do | 
|  | 
| 90 | 77 | 
 | 
| 91 | 78 |     it 'calls the right command to upload the pipeline without env_file' do | 
| 92 | 79 |       expect(Fastlane::Action).to receive(:sh).with( | 
| 93 |  | -        { 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default }, | 
|  | 80 | +        environment_default, | 
| 94 | 81 |         'buildkite-agent', 'pipeline', 'upload', pipeline_file | 
| 95 | 82 |       ) | 
| 96 | 83 |       expect_upload_pipeline_message | 
| 97 | 84 | 
 | 
| 98 |  | -      run_described_fastlane_action( | 
| 99 |  | -        pipeline_file: pipeline_file, | 
| 100 |  | -        branch: branch | 
| 101 |  | -      ) | 
|  | 85 | +      run_described_fastlane_action(pipeline_file: pipeline_file) | 
| 102 | 86 |     end | 
| 103 | 87 | 
 | 
| 104 | 88 |     it 'calls the right command to upload the pipeline with env_file' do | 
| 105 | 89 |       allow(File).to receive(:exist?).with(env_file).and_return(true) | 
| 106 | 90 |       expect(Fastlane::Action).to receive(:sh).with( | 
| 107 |  | -        { 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default }, | 
|  | 91 | +        environment_default, | 
| 108 | 92 |         "source #{env_file.shellescape} && buildkite-agent pipeline upload #{pipeline_file.shellescape}" | 
| 109 | 93 |       ) | 
| 110 | 94 |       expect_upload_pipeline_message | 
| 111 |  | -      expect(Fastlane::UI).to receive(:message).with(/Sourcing environment file beforehand: #{env_file}/) | 
|  | 95 | +      expect_sourcing_env_file_message(env_file) | 
| 112 | 96 | 
 | 
| 113 | 97 |       run_described_fastlane_action( | 
| 114 | 98 |         pipeline_file: pipeline_file, | 
| 115 |  | -        env_file: env_file, | 
| 116 |  | -        branch: branch | 
|  | 99 | +        env_file: env_file | 
| 117 | 100 |       ) | 
| 118 | 101 |     end | 
| 119 | 102 | 
 | 
| 120 | 103 |     it 'skips sourcing env_file when it does not exist' do | 
| 121 | 104 |       non_existent_env_file = 'path/to/non_existent_env_file' | 
| 122 | 105 |       allow(File).to receive(:exist?).with(non_existent_env_file).and_return(false) | 
| 123 | 106 |       expect(Fastlane::Action).to receive(:sh).with( | 
| 124 |  | -        { 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default }, | 
|  | 107 | +        environment_default, | 
| 125 | 108 |         'buildkite-agent', 'pipeline', 'upload', pipeline_file | 
| 126 | 109 |       ) | 
| 127 | 110 |       expect(Fastlane::UI).not_to receive(:message).with(/Sourcing environment file/) | 
| 128 | 111 | 
 | 
| 129 | 112 |       run_described_fastlane_action( | 
| 130 | 113 |         pipeline_file: pipeline_file, | 
| 131 |  | -        env_file: non_existent_env_file, | 
| 132 |  | -        branch: branch | 
|  | 114 | +        env_file: non_existent_env_file | 
| 133 | 115 |       ) | 
| 134 | 116 |     end | 
| 135 | 117 | 
 | 
| 136 | 118 |     it 'uses a default env_file when no env_file is provided' do | 
| 137 | 119 |       allow(File).to receive(:exist?).with(env_file_default).and_return(true) | 
| 138 | 120 |       expect(Fastlane::Action).to receive(:sh).with( | 
| 139 |  | -        { 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default }, | 
|  | 121 | +        environment_default, | 
| 140 | 122 |         "source #{env_file_default} && buildkite-agent pipeline upload #{pipeline_file.shellescape}" | 
| 141 | 123 |       ) | 
| 142 | 124 |       expect_upload_pipeline_message | 
| 143 |  | -      expect(Fastlane::UI).to receive(:message).with(/Sourcing environment file beforehand: #{env_file_default}/) | 
|  | 125 | +      expect_sourcing_env_file_message(env_file_default) | 
| 144 | 126 | 
 | 
| 145 | 127 |       run_described_fastlane_action( | 
| 146 |  | -        pipeline_file: pipeline_file, | 
| 147 |  | -        branch: branch | 
|  | 128 | +        pipeline_file: pipeline_file | 
| 148 | 129 |       ) | 
| 149 | 130 |     end | 
| 150 | 131 |   end | 
|  | 
| 156 | 137 | 
 | 
| 157 | 138 |       expect do | 
| 158 | 139 |         run_described_fastlane_action( | 
| 159 |  | -          pipeline_file: pipeline_file, | 
| 160 |  | -          branch: branch | 
|  | 140 | +          pipeline_file: pipeline_file | 
| 161 | 141 |         ) | 
| 162 | 142 |       end.to raise_error(StandardError, 'Upload failed') | 
| 163 | 143 |     end | 
| 164 | 144 |   end | 
| 165 | 145 | 
 | 
| 166 |  | -  def expect_upload_pipeline_message(expected_branch: branch, expected_commit: commit_default) | 
|  | 146 | +  def expect_upload_pipeline_message | 
|  | 147 | +    expect(Fastlane::UI).to receive(:message).with( | 
|  | 148 | +      "Adding steps from `#{pipeline_file}` to the current build" | 
|  | 149 | +    ) | 
|  | 150 | +  end | 
|  | 151 | + | 
|  | 152 | +  def expect_sourcing_env_file_message(env_file) | 
| 167 | 153 |     expect(Fastlane::UI).to receive(:message).with( | 
| 168 |  | -      "Adding steps from `#{pipeline_file}` to the current build (#{expected_branch ? "branch: `#{expected_branch}`, " : ''}commit: `#{expected_commit}`)" | 
|  | 154 | +      /Sourcing environment file beforehand: #{env_file}/ | 
| 169 | 155 |     ) | 
| 170 | 156 |   end | 
| 171 | 157 | end | 
0 commit comments