From 4c5468b40825debc2b7bbe08b975dedd2f7f1523 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 24 Jun 2020 15:08:50 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- spec/lib/gitlab/ci/build/releaser_spec.rb | 16 +- spec/lib/gitlab/ci/config/entry/release_spec.rb | 203 +++++++++++++++++----- spec/lib/gitlab/ci/yaml_processor_spec.rb | 3 + spec/lib/gitlab/diff/stats_cache_spec.rb | 84 +++++++++ spec/lib/gitlab/git/diff_stats_collection_spec.rb | 4 +- 5 files changed, 257 insertions(+), 53 deletions(-) create mode 100644 spec/lib/gitlab/diff/stats_cache_spec.rb (limited to 'spec/lib/gitlab') diff --git a/spec/lib/gitlab/ci/build/releaser_spec.rb b/spec/lib/gitlab/ci/build/releaser_spec.rb index b338cd3a2d2..8c29c08db78 100644 --- a/spec/lib/gitlab/ci/build/releaser_spec.rb +++ b/spec/lib/gitlab/ci/build/releaser_spec.rb @@ -13,13 +13,15 @@ describe Gitlab::Ci::Build::Releaser do name: 'Release $CI_COMMIT_SHA', description: 'Created using the release-cli $EXTRA_DESCRIPTION', tag_name: 'release-$CI_COMMIT_SHA', - ref: '$CI_COMMIT_SHA' + ref: '$CI_COMMIT_SHA', + milestones: %w[m1 m2 m3], + released_at: '2020-07-15T08:00:00Z' } } end it 'generates the script' do - expect(subject).to eq(['release-cli create --name "Release $CI_COMMIT_SHA" --description "Created using the release-cli $EXTRA_DESCRIPTION" --tag-name "release-$CI_COMMIT_SHA" --ref "$CI_COMMIT_SHA"']) + expect(subject).to eq(['release-cli create --name "Release $CI_COMMIT_SHA" --description "Created using the release-cli $EXTRA_DESCRIPTION" --tag-name "release-$CI_COMMIT_SHA" --ref "$CI_COMMIT_SHA" --released-at "2020-07-15T08:00:00Z" --milestone "m1" --milestone "m2" --milestone "m3"']) end end @@ -27,10 +29,12 @@ describe Gitlab::Ci::Build::Releaser do using RSpec::Parameterized::TableSyntax where(:node_name, :node_value, :result) do - 'name' | 'Release $CI_COMMIT_SHA' | 'release-cli create --name "Release $CI_COMMIT_SHA"' - 'description' | 'Release-cli $EXTRA_DESCRIPTION' | 'release-cli create --description "Release-cli $EXTRA_DESCRIPTION"' - 'tag_name' | 'release-$CI_COMMIT_SHA' | 'release-cli create --tag-name "release-$CI_COMMIT_SHA"' - 'ref' | '$CI_COMMIT_SHA' | 'release-cli create --ref "$CI_COMMIT_SHA"' + :name | 'Release $CI_COMMIT_SHA' | 'release-cli create --name "Release $CI_COMMIT_SHA"' + :description | 'Release-cli $EXTRA_DESCRIPTION' | 'release-cli create --description "Release-cli $EXTRA_DESCRIPTION"' + :tag_name | 'release-$CI_COMMIT_SHA' | 'release-cli create --tag-name "release-$CI_COMMIT_SHA"' + :ref | '$CI_COMMIT_SHA' | 'release-cli create --ref "$CI_COMMIT_SHA"' + :milestones | %w[m1 m2 m3] | 'release-cli create --milestone "m1" --milestone "m2" --milestone "m3"' + :released_at | '2020-07-15T08:00:00Z' | 'release-cli create --released-at "2020-07-15T08:00:00Z"' end with_them do diff --git a/spec/lib/gitlab/ci/config/entry/release_spec.rb b/spec/lib/gitlab/ci/config/entry/release_spec.rb index 1559a7467b3..790ed160d15 100644 --- a/spec/lib/gitlab/ci/config/entry/release_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/release_spec.rb @@ -5,21 +5,32 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Config::Entry::Release do let(:entry) { described_class.new(config) } + shared_examples_for 'a valid entry' do + describe '#value' do + it 'returns release configuration' do + expect(entry.value).to eq config + end + end + + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid + end + end + end + + shared_examples_for 'reports error' do |message| + it 'reports error' do + expect(entry.errors) + .to include message + end + end + describe 'validation' do context 'when entry config value is correct' do let(:config) { { tag_name: 'v0.06', description: "./release_changelog.txt" } } - describe '#value' do - it 'returns release configuration' do - expect(entry.value).to eq config - end - end - - describe '#valid?' do - it 'is valid' do - expect(entry).to be_valid - end - end + it_behaves_like 'a valid entry' end context "when value includes 'assets' keyword" do @@ -36,38 +47,116 @@ RSpec.describe Gitlab::Ci::Config::Entry::Release do } end - describe '#value' do - it 'returns release configuration' do - expect(entry.value).to eq config - end + it_behaves_like 'a valid entry' + end + + context "when value includes 'name' keyword" do + let(:config) do + { + tag_name: 'v0.06', + description: "./release_changelog.txt", + name: "Release $CI_TAG_NAME" + } end - describe '#valid?' do - it 'is valid' do - expect(entry).to be_valid - end + it_behaves_like 'a valid entry' + end + + context "when value includes 'ref' keyword" do + let(:config) do + { + tag_name: 'v0.06', + description: "./release_changelog.txt", + name: "Release $CI_TAG_NAME", + ref: 'b3235930aa443112e639f941c69c578912189bdd' + } end + + it_behaves_like 'a valid entry' end - context "when value includes 'name' keyword" do + context "when value includes 'released_at' keyword" do let(:config) do { tag_name: 'v0.06', description: "./release_changelog.txt", - name: "Release $CI_TAG_NAME" + name: "Release $CI_TAG_NAME", + released_at: '2019-03-15T08:00:00Z' } end - describe '#value' do - it 'returns release configuration' do - expect(entry.value).to eq config - end + it_behaves_like 'a valid entry' + end + + context "when value includes 'milestones' keyword" do + let(:config) do + { + tag_name: 'v0.06', + description: "./release_changelog.txt", + name: "Release $CI_TAG_NAME", + milestones: milestones + } end - describe '#valid?' do - it 'is valid' do - expect(entry).to be_valid - end + context 'for an array of milestones' do + let(:milestones) { %w[m1 m2 m3] } + + it_behaves_like 'a valid entry' + end + + context 'for a single milestone' do + let(:milestones) { 'm1' } + + it_behaves_like 'a valid entry' + end + end + + context "when value includes 'ref' keyword" do + let(:config) do + { + tag_name: 'v0.06', + description: "./release_changelog.txt", + name: "Release $CI_TAG_NAME", + ref: 'b3235930aa443112e639f941c69c578912189bdd' + } + end + + it_behaves_like 'a valid entry' + end + + context "when value includes 'released_at' keyword" do + let(:config) do + { + tag_name: 'v0.06', + description: "./release_changelog.txt", + name: "Release $CI_TAG_NAME", + released_at: '2019-03-15T08:00:00Z' + } + end + + it_behaves_like 'a valid entry' + end + + context "when value includes 'milestones' keyword" do + let(:config) do + { + tag_name: 'v0.06', + description: "./release_changelog.txt", + name: "Release $CI_TAG_NAME", + milestones: milestones + } + end + + context 'for an array of milestones' do + let(:milestones) { %w[m1 m2 m3] } + + it_behaves_like 'a valid entry' + end + + context 'for a single milestone' do + let(:milestones) { 'm1' } + + it_behaves_like 'a valid entry' end end @@ -76,37 +165,61 @@ RSpec.describe Gitlab::Ci::Config::Entry::Release do context 'when value of attribute is invalid' do let(:config) { { description: 10 } } - it 'reports error' do - expect(entry.errors) - .to include 'release description should be a string' - end + it_behaves_like 'reports error', 'release description should be a string' end context 'when release description is missing' do let(:config) { { tag_name: 'v0.06' } } - it 'reports error' do - expect(entry.errors) - .to include "release description can't be blank" - end + it_behaves_like 'reports error', "release description can't be blank" end context 'when release tag_name is missing' do let(:config) { { description: "./release_changelog.txt" } } - it 'reports error' do - expect(entry.errors) - .to include "release tag name can't be blank" - end + it_behaves_like 'reports error', "release tag name can't be blank" end context 'when there is an unknown key present' do let(:config) { { test: 100 } } - it 'reports error' do - expect(entry.errors) - .to include 'release config contains unknown keys: test' - end + it_behaves_like 'reports error', 'release config contains unknown keys: test' + end + + context 'when `released_at` is not a valid date' do + let(:config) { { released_at: 'ABC123' } } + + it_behaves_like 'reports error', 'release released at must be a valid datetime' + end + + context 'when `ref` is not valid' do + let(:config) { { ref: 'ABC123' } } + + it_behaves_like 'reports error', 'release ref must be a valid ref' + end + + context 'when `milestones` is not an array of strings' do + let(:config) { { milestones: [1, 2, 3] } } + + it_behaves_like 'reports error', 'release milestones should be an array of strings or a string' + end + + context 'when `released_at` is not a valid date' do + let(:config) { { released_at: 'ABC123' } } + + it_behaves_like 'reports error', 'release released at must be a valid datetime' + end + + context 'when `ref` is not valid' do + let(:config) { { ref: 'ABC123' } } + + it_behaves_like 'reports error', 'release ref must be a valid ref' + end + + context 'when `milestones` is not an array of strings' do + let(:config) { { milestones: [1, 2, 3] } } + + it_behaves_like 'reports error', 'release milestones should be an array of strings or a string' end end end diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index 1668149d8f5..07f3bd0917d 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -1397,6 +1397,9 @@ module Gitlab tag_name: "$CI_COMMIT_TAG", name: "Release $CI_TAG_NAME", description: "./release_changelog.txt", + ref: 'b3235930aa443112e639f941c69c578912189bdd', + released_at: '2019-03-15T08:00:00Z', + milestones: %w[m1 m2 m3], assets: { links: [ { diff --git a/spec/lib/gitlab/diff/stats_cache_spec.rb b/spec/lib/gitlab/diff/stats_cache_spec.rb new file mode 100644 index 00000000000..1de6fa0aab4 --- /dev/null +++ b/spec/lib/gitlab/diff/stats_cache_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Diff::StatsCache, :use_clean_rails_memory_store_caching do + subject(:stats_cache) { described_class.new(cachable_key: cachable_key) } + + let(:key) { ['diff_stats', cachable_key, described_class::VERSION].join(":") } + let(:cachable_key) { 'cachecachecache' } + let(:stat) { Gitaly::DiffStats.new(path: 'temp', additions: 10, deletions: 15) } + let(:stats) { Gitlab::Git::DiffStatsCollection.new([stat]) } + let(:cache) { Rails.cache } + + describe '#read' do + before do + stats_cache.write_if_empty(stats) + end + + it 'returns the expected stats' do + expect(stats_cache.read.to_json).to eq(stats.to_json) + end + end + + describe '#write_if_empty' do + context 'when the cache already exists' do + before do + Rails.cache.write(key, true) + end + + it 'does not write the stats' do + expect(cache).not_to receive(:write) + + stats_cache.write_if_empty(stats) + end + end + + context 'when the cache does not exist' do + it 'writes the stats' do + expect(cache) + .to receive(:write) + .with(key, stats.as_json, expires_in: described_class::EXPIRATION) + .and_call_original + + stats_cache.write_if_empty(stats) + + expect(stats_cache.read.to_a).to eq(stats.to_a) + end + + context 'when given non utf-8 characters' do + let(:non_utf8_path) { '你好'.b } + let(:stat) { Gitaly::DiffStats.new(path: non_utf8_path, additions: 10, deletions: 15) } + + it 'writes the stats' do + expect(cache) + .to receive(:write) + .with(key, stats.as_json, expires_in: described_class::EXPIRATION) + .and_call_original + + stats_cache.write_if_empty(stats) + + expect(stats_cache.read.to_a).to eq(stats.to_a) + end + end + + context 'when given empty stats' do + let(:stats) { nil } + + it 'does not write the stats' do + expect(cache).not_to receive(:write) + + stats_cache.write_if_empty(stats) + end + end + end + end + + describe '#clear' do + it 'clears cache' do + expect(cache).to receive(:delete).with(key) + + stats_cache.clear + end + end +end diff --git a/spec/lib/gitlab/git/diff_stats_collection_spec.rb b/spec/lib/gitlab/git/diff_stats_collection_spec.rb index 82d15a49062..46bff68e12a 100644 --- a/spec/lib/gitlab/git/diff_stats_collection_spec.rb +++ b/spec/lib/gitlab/git/diff_stats_collection_spec.rb @@ -4,11 +4,11 @@ require "spec_helper" describe Gitlab::Git::DiffStatsCollection do let(:stats_a) do - double(Gitaly::DiffStats, additions: 10, deletions: 15, path: 'foo') + Gitaly::DiffStats.new(additions: 10, deletions: 15, path: 'foo') end let(:stats_b) do - double(Gitaly::DiffStats, additions: 5, deletions: 1, path: 'bar') + Gitaly::DiffStats.new(additions: 5, deletions: 1, path: 'bar') end let(:diff_stats) { [stats_a, stats_b] } -- cgit v1.2.3