Commit 07d6cbfb authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'pl-spec-factories-go-module' into 'master'

Fix some remaining cop offenses for go module factories

See merge request gitlab-org/gitlab!55772
parents 48878e18 002dd7be
...@@ -7,7 +7,12 @@ FactoryBot.define do ...@@ -7,7 +7,12 @@ FactoryBot.define do
transient do transient do
files { { 'foo.txt' => 'content' } } files { { 'foo.txt' => 'content' } }
message { 'Message' } message { 'Message' }
# rubocop: disable FactoryBot/InlineAssociation
# We need a persisted project so we can create commits and tags
# in `commit` otherwise linting this factory with `build` strategy
# will fail.
project { create(:project, :repository) } project { create(:project, :repository) }
# rubocop: enable FactoryBot/InlineAssociation
service do service do
Files::MultiService.new( Files::MultiService.new(
...@@ -44,14 +49,13 @@ FactoryBot.define do ...@@ -44,14 +49,13 @@ FactoryBot.define do
trait :files do trait :files do
transient do transient do
files { raise ArgumentError.new("files is required") }
message { 'Add files' } message { 'Add files' }
end end
end end
trait :package do trait :package do
transient do transient do
path { raise ArgumentError.new("path is required") } path { 'pkg' }
message { 'Add package' } message { 'Add package' }
files { { "#{path}/b.go" => "package b\nfunc Bye() { println(\"Goodbye world!\") }\n" } } files { { "#{path}/b.go" => "package b\nfunc Bye() { println(\"Goodbye world!\") }\n" } }
end end
...@@ -64,7 +68,7 @@ FactoryBot.define do ...@@ -64,7 +68,7 @@ FactoryBot.define do
host_prefix { "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}" } host_prefix { "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}" }
url { name ? "#{host_prefix}/#{name}" : host_prefix } url { name ? "#{host_prefix}/#{name}" : host_prefix }
path { name.to_s + '/' } path { "#{name}/" }
files do files do
{ {
......
...@@ -8,12 +8,12 @@ FactoryBot.define do ...@@ -8,12 +8,12 @@ FactoryBot.define do
p = attributes[:params] p = attributes[:params]
s = Packages::SemVer.parse(p.semver, prefixed: true) s = Packages::SemVer.parse(p.semver, prefixed: true)
raise ArgumentError.new("invalid sematic version: '#{p.semver}''") if !s && p.semver raise ArgumentError, "invalid sematic version: '#{p.semver}'" if !s && p.semver
new(p.mod, p.type, p.commit, name: p.name, semver: s, ref: p.ref) new(p.mod, p.type, p.commit, name: p.name, semver: s, ref: p.ref)
end end
mod { create :go_module } mod { association(:go_module) }
type { :commit } type { :commit }
commit { mod.project.repository.head_commit } commit { mod.project.repository.head_commit }
name { nil } name { nil }
...@@ -33,45 +33,11 @@ FactoryBot.define do ...@@ -33,45 +33,11 @@ FactoryBot.define do
mod.project.repository.tags mod.project.repository.tags
.filter { |t| Packages::SemVer.match?(t.name, prefixed: true) } .filter { |t| Packages::SemVer.match?(t.name, prefixed: true) }
.map { |t| Packages::SemVer.parse(t.name, prefixed: true) } .map { |t| Packages::SemVer.parse(t.name, prefixed: true) }
.max { |a, b| "#{a}" <=> "#{b}" } .max_by(&:to_s)
.to_s .to_s
end end
params { OpenStruct.new(mod: mod, type: :ref, commit: commit, semver: name, ref: ref) } params { OpenStruct.new(mod: mod, type: :ref, commit: commit, semver: name, ref: ref) }
end end
trait :pseudo do
transient do
prefix do
# This provides a sane default value, but in reality the caller should
# specify `prefix:`
# This does not take into account that `commit` may be before the
# latest tag.
# Find 'latest' semver tag (does not actually use semver precedence rules)
v = mod.project.repository.tags
.filter { |t| Packages::SemVer.match?(t.name, prefixed: true) }
.map { |t| Packages::SemVer.parse(t.name, prefixed: true) }
.max { |a, b| "#{a}" <=> "#{b}" }
# Default if no semver tags exist
next 'v0.0.0' unless v
# Valid pseudo-versions are:
# vX.0.0-yyyymmddhhmmss-sha1337beef0, when no earlier tagged commit exists for X
# vX.Y.Z-pre.0.yyyymmddhhmmss-sha1337beef0, when most recent prior tag is vX.Y.Z-pre
# vX.Y.(Z+1)-0.yyyymmddhhmmss-sha1337beef0, when most recent prior tag is vX.Y.Z
v = v.with(patch: v.patch + 1) unless v.prerelease
"#{v}.0"
end
end
type { :pseudo }
name { "#{prefix}#{commit.committed_date.strftime('%Y%m%d%H%M%S')}-#{commit.sha[0..11]}" }
params { OpenStruct.new(mod: mod, type: :pseudo, commit: commit, name: name, semver: name) }
end
end end
end end
...@@ -5,7 +5,7 @@ FactoryBot.define do ...@@ -5,7 +5,7 @@ FactoryBot.define do
initialize_with { new(attributes[:project], attributes[:name], attributes[:path]) } initialize_with { new(attributes[:project], attributes[:name], attributes[:path]) }
skip_create skip_create
project { create :project, :repository } project { association(:project, :repository) }
path { '' } path { '' }
name { "#{Settings.build_gitlab_go_url}/#{project.full_path}#{path.empty? ? '' : '/'}#{path}" } name { "#{Settings.build_gitlab_go_url}/#{project.full_path}#{path.empty? ? '' : '/'}#{path}" }
......
...@@ -18,9 +18,6 @@ RSpec.describe 'factories' do ...@@ -18,9 +18,6 @@ RSpec.describe 'factories' do
[:ci_job_artifact, :gzip], [:ci_job_artifact, :gzip],
[:ci_job_artifact, :correct_checksum], [:ci_job_artifact, :correct_checksum],
[:environment, :non_playable], [:environment, :non_playable],
[:go_module_commit, :files],
[:go_module_commit, :package],
[:go_module_version, :pseudo],
[:composer_cache_file, :object_storage], [:composer_cache_file, :object_storage],
[:debian_project_component_file, :object_storage], [:debian_project_component_file, :object_storage],
[:debian_project_distribution, :object_storage], [:debian_project_distribution, :object_storage],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment