summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/templates/npm_spec.rb
blob: a949a7ccfb1a2f03cb6ecc95d3cc40b214c25be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'npm.gitlab-ci.yml', feature_category: :continuous_integration do
  subject(:template) { Gitlab::Template::GitlabCiYmlTemplate.find('npm') }

  describe 'the created pipeline' do
    let(:repo_files) { { 'package.json' => '{}', 'README.md' => '' } }
    let(:modified_files) { %w[package.json] }
    let(:project) { create(:project, :custom_repo, files: repo_files) }
    let(:user) { project.first_owner }
    let(:pipeline_branch) { project.default_branch }
    let(:pipeline_tag) { 'v1.2.1' }
    let(:pipeline_ref) { pipeline_branch }
    let(:service) { Ci::CreatePipelineService.new(project, user, ref: pipeline_ref ) }
    let(:pipeline) { service.execute(:push).payload }
    let(:build_names) { pipeline.builds.pluck(:name) }

    def create_branch(name:)
      ::Branches::CreateService.new(project, user).execute(name, project.default_branch)
    end

    def create_tag(name:)
      ::Tags::CreateService.new(project, user).execute(name, project.default_branch, nil)
    end

    before do
      stub_ci_pipeline_yaml_file(template.content)

      create_branch(name: pipeline_branch)
      create_tag(name: pipeline_tag)

      allow_any_instance_of(Ci::Pipeline).to receive(:modified_paths).and_return(modified_files)
    end

    shared_examples 'publish job created' do
      it 'creates a pipeline with a single job: publish' do
        expect(build_names).to eq(%w[publish])
      end
    end

    shared_examples 'no pipeline created' do
      it 'does not create a pipeline because the only job (publish) is not created' do
        expect(build_names).to be_empty
        expect(pipeline.errors.full_messages).to match_array(['Pipeline will not run for the selected trigger. ' \
          'The rules configuration prevented any jobs from being added to the pipeline.'])
      end
    end

    context 'on default branch' do
      context 'when package.json has been changed' do
        it_behaves_like 'publish job created'
      end

      context 'when package.json does not exist or has not been changed' do
        let(:modified_files) { %w[README.md] }

        it_behaves_like 'no pipeline created'
      end
    end

    %w[v1.0.0 v2.1.0-alpha].each do |valid_version|
      context "when the branch name is #{valid_version}" do
        let(:pipeline_branch) { valid_version }

        it_behaves_like 'publish job created'
      end

      context "when the tag name is #{valid_version}" do
        let(:pipeline_tag) { valid_version }
        let(:pipeline_ref) { pipeline_tag }

        it_behaves_like 'publish job created'
      end
    end

    %w[patch-1 my-feature-branch v1 v1.0 2.1.0].each do |invalid_version|
      context "when the branch name is #{invalid_version}" do
        let(:pipeline_branch) { invalid_version }

        it_behaves_like 'no pipeline created'
      end

      context "when the tag name is #{invalid_version}" do
        let(:pipeline_tag) { invalid_version }
        let(:pipeline_ref) { pipeline_tag }

        it_behaves_like 'no pipeline created'
      end
    end
  end
end