diff --git a/.config/cucumber.yml b/.config/cucumber.yml new file mode 100644 index 00000000..fea5edc1 --- /dev/null +++ b/.config/cucumber.yml @@ -0,0 +1 @@ +default: --publish-quiet diff --git a/.rubocop.yml b/.rubocop.yml index 84fb5455..8c30206a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -25,6 +25,8 @@ Layout/IndentHeredoc: # sane line length Metrics/LineLength: Max: 120 + Exclude: + - 'features/**/*' Style/FrozenStringLiteralComment: Enabled: false diff --git a/features/cli.feature b/features/cli.feature index 509ba47c..f4ad61ed 100644 --- a/features/cli.feature +++ b/features/cli.feature @@ -18,7 +18,8 @@ Feature: CLI And the output should match /Commands:/ Scenario: When overriding a setting from the config file on the command line - Given a file named "managed_modules.yml" with: + Given a puppet module "puppet-test" from "fakenamespace" + And a file named "managed_modules.yml" with: """ --- - puppet-test @@ -26,10 +27,10 @@ Feature: CLI And a file named "modulesync.yml" with: """ --- - namespace: maestrodev - git_base: 'git@github.com:' + namespace: default """ + And a git_base option appended to "modulesync.yml" for local tests And a directory named "moduleroot" - When I run `msync update --noop --git-base https://github.com/` + When I run `msync update --noop --namespace fakenamespace` Then the exit status should be 0 - And the output should not match /git@github.com:/ + And the output should match /Syncing fakenamespace/ diff --git a/features/hook.feature b/features/hook.feature index 10e97909..cae008e9 100644 --- a/features/hook.feature +++ b/features/hook.feature @@ -5,8 +5,7 @@ Feature: hook Given a directory named ".git/hooks" When I run `msync hook activate` Then the exit status should be 0 - Given I run `cat .git/hooks/pre-push` - Then the output should contain "bash" + And the file named ".git/hooks/pre-push" should contain "bash" Scenario: Deactivating a hook Given a file named ".git/hooks/pre-push" with: @@ -21,8 +20,7 @@ Feature: hook Given a directory named ".git/hooks" When I run `msync hook activate -a '--foo bar --baz quux' -b master` Then the exit status should be 0 - Given I run `cat .git/hooks/pre-push` - Then the output should match: + And the file named ".git/hooks/pre-push" should contain: """ - "\$message" -n puppetlabs -b master --foo bar --baz quux + "$message" -n puppetlabs -b master --foo bar --baz quux """ diff --git a/features/step_definitions/git_steps.rb b/features/step_definitions/git_steps.rb index 36c1e903..2db998f7 100644 --- a/features/step_definitions/git_steps.rb +++ b/features/step_definitions/git_steps.rb @@ -1,48 +1,75 @@ -Given 'a mocked git configuration' do - steps %( - Given a mocked home directory - And I run `git config --global user.name Test` - And I run `git config --global user.email test@example.com` - ) -end +require_relative '../../spec/helpers/faker/puppet_module_remote_repo' -Given 'a remote module repository' do +Given 'a basic setup with a puppet module {string} from {string}' do |name, namespace| steps %( - Given a directory named "sources" - And I run `git clone https://github.com/maestrodev/puppet-test sources/puppet-test` + Given a mocked git configuration + And a puppet module "#{name}" from "#{namespace}" And a file named "managed_modules.yml" with: """ --- - - puppet-test + - #{name} """ + And a file named "modulesync.yml" with: + """ + --- + namespace: #{namespace} + """ + And a git_base option appended to "modulesync.yml" for local tests ) - write_file('modulesync.yml', <<~CONFIG) - --- - namespace: sources - git_base: file://#{expand_path('.')}/ - CONFIG end -Given Regexp.new(/a remote module repository with "(.+?)" as the default branch/) do |branch| +Given 'a mocked git configuration' do steps %( - Given a directory named "sources" - And I run `git clone --mirror https://github.com/maestrodev/puppet-test sources/puppet-test` - And a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ + Given a mocked home directory + And I run `git config --global user.name Aruba` + And I run `git config --global user.email aruba@example.com` ) - write_file('modulesync.yml', <<~CONFIG) - --- - namespace: sources - git_base: file://#{expand_path('.')}/ - CONFIG - - cd('sources/puppet-test') do - steps %( - And I run `git branch -M master #{branch}` - And I run `git symbolic-ref HEAD refs/heads/#{branch}` - ) - end +end + +Given 'a puppet module {string} from {string}' do |name, namespace| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + pmrr.populate +end + +Given 'a git_base option appended to "modulesync.yml" for local tests' do + File.write "#{Aruba.config.working_directory}/modulesync.yml", "\ngit_base: #{ModuleSync::Faker::PuppetModuleRemoteRepo.git_base}", mode: 'a' +end + +Given 'the puppet module {string} from {string} is read-only' do |name, namespace| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + pmrr.read_only = true +end + +Then 'the puppet module {string} from {string} should have no commits between {string} and {string}' do |name, namespace, commit1, commit2| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + expect(pmrr.commit_count_between(commit1, commit2)).to eq 0 +end + +Then 'the puppet module {string} from {string} should have( only) {int} commit(s) made by {string}' do |name, namespace, commit_count, author| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + expect(pmrr.commit_count_by(author)).to eq commit_count +end + +Then 'the puppet module {string} from {string} should have( only) {int} commit(s) made by {string} in branch {string}' do |name, namespace, commit_count, author, branch| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + expect(pmrr.commit_count_by(author, branch)).to eq commit_count +end + +Then 'the puppet module {string} from {string} should have no commits made by {string}' do |name, namespace, author| + step "the puppet module \"#{name}\" from \"#{namespace}\" should have 0 commits made by \"#{author}\"" +end + +Given 'the puppet module {string} from {string} has a file named {string} with:' do |name, namespace, filename, content| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + pmrr.add_file(filename, content) +end + +Then 'the puppet module {string} from {string} should have a branch {string} with a file named {string} which contains:' do |name, namespace, branch, filename, content| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + expect(pmrr.read_file(filename, branch)).to include(content) +end + +Given 'the puppet module {string} from {string} has the default branch named {string}' do |name, namespace, default_branch| + pmrr = ModuleSync::Faker::PuppetModuleRemoteRepo.new(name, namespace) + pmrr.default_branch = default_branch end diff --git a/features/support/env.rb b/features/support/env.rb index f3f544d6..de44b9d4 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,5 +1,9 @@ require 'aruba/cucumber' +require_relative '../../spec/helpers/faker' + +ModuleSync::Faker.working_directory = File.expand_path('faker', Aruba.config.working_directory) + Before do @aruba_timeout_seconds = 5 end diff --git a/features/update.feature b/features/update.feature index 5dedf977..dd096cf7 100644 --- a/features/update.feature +++ b/features/update.feature @@ -2,17 +2,7 @@ Feature: update ModuleSync needs to update module boilerplate Scenario: Adding a new file - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -31,21 +21,11 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" Scenario: Using skip_broken option and adding a new file to repo without write access - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: 'git@github.com:' - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And the puppet module "puppet-test" from "fakenamespace" is read-only And a file named "config_defaults.yml" with: """ --- @@ -59,19 +39,11 @@ Feature: update """ When I run `msync update -s -m "Add test"` Then the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Adding a new file to repo without write access - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: 'git@github.com:' - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And the puppet module "puppet-test" from "fakenamespace" is read-only And a file named "config_defaults.yml" with: """ --- @@ -85,19 +57,10 @@ Feature: update """ When I run `msync update -m "Add test" -r` Then the exit status should be 1 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Adding a new file, without the .erb suffix - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -120,21 +83,11 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Adding a new file using global values - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -153,21 +106,11 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Adding a new file overriding global values - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -189,21 +132,11 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Adding a new file ignoring global values - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -225,21 +158,11 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Adding a file that ERB can't parse - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -255,19 +178,10 @@ Feature: update """ When I run `msync update --noop` Then the exit status should be 1 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Using skip_broken option with invalid files - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -283,19 +197,10 @@ Feature: update """ When I run `msync update --noop -s` Then the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Using skip_broken and fail_on_warnings options with invalid files - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -311,18 +216,13 @@ Feature: update """ When I run `msync update --noop --skip_broken --fail_on_warnings` Then the exit status should be 1 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Modifying an existing file - Given a file named "managed_modules.yml" with: + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And the puppet module "puppet-test" from "fakenamespace" has a file named "Gemfile" with: """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ + source 'https://example.com' """ And a file named "config_defaults.yml" with: """ @@ -342,15 +242,18 @@ Feature: update Files changed: +diff --git a/Gemfile b/Gemfile """ - Given I run `cat modules/maestrodev/puppet-test/Gemfile` - Then the output should contain: + And the file named "modules/fakenamespace/puppet-test/Gemfile" should contain: """ source 'https://somehost.com' """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Modifying an existing file and committing the change - Given a mocked git configuration - And a remote module repository + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And the puppet module "puppet-test" from "fakenamespace" has a file named "Gemfile" with: + """ + source 'https://example.com' + """ And a file named "config_defaults.yml" with: """ --- @@ -364,30 +267,25 @@ Feature: update """ When I run `msync update -m "Update Gemfile" -r test` Then the exit status should be 0 - Given I cd to "sources/puppet-test" - And I run `git checkout test` - Then the file "Gemfile" should contain: + And the puppet module "puppet-test" from "fakenamespace" should have only 1 commit made by "Aruba" + And the puppet module "puppet-test" from "fakenamespace" should have 1 commit made by "Aruba" in branch "test" + And the puppet module "puppet-test" from "fakenamespace" should have a branch "test" with a file named "Gemfile" which contains: """ source 'https://somehost.com' """ Scenario: Setting an existing file to unmanaged - Given a file named "managed_modules.yml" with: + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And the puppet module "puppet-test" from "fakenamespace" has a file named "Gemfile" with: """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ + source 'https://rubygems.org' """ And a file named "config_defaults.yml" with: """ --- Gemfile: unmanaged: true + gem_source: https://somehost.com """ And a directory named "moduleroot" And a file named "moduleroot/Gemfile.erb" with: @@ -405,24 +303,14 @@ Feature: update Not managing Gemfile in puppet-test """ And the exit status should be 0 - Given I run `cat modules/maestrodev/puppet-test/Gemfile` - Then the output should contain: + And the file named "modules/fakenamespace/puppet-test/Gemfile" should contain: """ source 'https://rubygems.org' """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Setting an existing file to deleted - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -434,6 +322,10 @@ Feature: update """ source '<%= @configs['gem_source'] %>' """ + And the puppet module "puppet-test" from "fakenamespace" has a file named "Gemfile" with: + """ + source 'https://rubygems.org' + """ When I run `msync update --noop` Then the output should match: """ @@ -442,19 +334,10 @@ Feature: update deleted file mode 100644 """ And the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Setting a non-existent file to deleted - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -464,19 +347,10 @@ Feature: update And a directory named "moduleroot" When I run `msync update -m 'deletes a file that doesnt exist!' -f puppet-test` And the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Setting a directory to unmanaged - Given a file named "managed_modules.yml" with: - """ - --- - - puppetlabs-apache - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: puppetlabs - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-apache" from "puppetlabs" And a file named "config_defaults.yml" with: """ --- @@ -488,36 +362,26 @@ Feature: update """ some spec_helper fud """ - And a directory named "modules/puppetlabs/puppetlabs-apache/spec" - And a file named "modules/puppetlabs/puppetlabs-apache/spec/spec_helper.rb" with: + And a directory named "modules/puppetlabs/puppet-apache/spec" + And a file named "modules/puppetlabs/puppet-apache/spec/spec_helper.rb" with: """ This is a fake spec_helper! """ When I run `msync update --offline` Then the output should contain: """ - Not managing spec/spec_helper.rb in puppetlabs-apache + Not managing spec/spec_helper.rb in puppet-apache """ And the exit status should be 0 - Given I run `cat modules/puppetlabs/puppetlabs-apache/spec/spec_helper.rb` - Then the output should contain: + And the file named "modules/puppetlabs/puppet-apache/spec/spec_helper.rb" should contain: """ This is a fake spec_helper! """ And the exit status should be 0 + And the puppet module "puppet-apache" from "puppetlabs" should have no commits made by "Aruba" Scenario: Adding a new file in a new subdirectory - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -538,24 +402,14 @@ Feature: update Files added: spec/spec_helper.rb """ - Given I run `cat modules/maestrodev/puppet-test/spec/spec_helper.rb` - Then the output should contain: + And the file named "modules/fakenamespace/puppet-test/spec/spec_helper.rb" should contain: """ require 'puppetlabs_spec_helper/module_helper' """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Updating offline - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -572,19 +426,14 @@ Feature: update When I run `msync update --offline` Then the exit status should be 0 And the output should not match /Files (changed|added|deleted):/ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Pulling a module that already exists in the modules directory - Given a file named "managed_modules.yml" with: - """ - --- - - maestrodev/puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - git_base: https://github.com/ - """ - And a file named "config_defaults.yml" with: + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + When I run `msync update` + Then the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" + Given a file named "config_defaults.yml" with: """ --- spec/spec_helper.rb: @@ -597,20 +446,6 @@ Feature: update require '<%= required %>' <% end %> """ - Given I run `git init modules/maestrodev/puppet-test` - Given a file named "modules/maestrodev/puppet-test/.git/config" with: - """ - [core] - repositoryformatversion = 0 - filemode = true - bare = false - logallrefupdates = true - ignorecase = true - precomposeunicode = true - [remote "origin"] - url = https://github.com/maestrodev/puppet-test.git - fetch = +refs/heads/*:refs/remotes/origin/* - """ When I run `msync update --noop` Then the exit status should be 0 And the output should match: @@ -618,37 +453,23 @@ Feature: update Files added: spec/spec_helper.rb """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: When running update with no changes - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a directory named "moduleroot" - When I run `msync update --noop` + When I run `msync update` Then the exit status should be 0 - And the output should not match /diff/ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: When specifying configurations in managed_modules.yml - Given a file named "managed_modules.yml" with: + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And a file named "managed_modules.yml" with: """ --- puppet-test: module_name: test """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ And a file named "config_defaults.yml" with: """ --- @@ -667,11 +488,14 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: When specifying configurations in managed_modules.yml and using a filter - Given a file named "managed_modules.yml" with: + Given a mocked git configuration + And a puppet module "puppet-test" from "fakenamespace" + And a puppet module "puppet-blacksmith" from "fakenamespace" + And a file named "managed_modules.yml" with: """ --- puppet-blacksmith: @@ -681,9 +505,9 @@ Feature: update And a file named "modulesync.yml" with: """ --- - namespace: maestrodev - git_base: https://github.com/ + namespace: fakenamespace """ + And a git_base option appended to "modulesync.yml" for local tests And a file named "config_defaults.yml" with: """ --- @@ -702,12 +526,15 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" - And a directory named "modules/maestrodev/puppet-blacksmith" should not exist + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And a directory named "modules/fakenamespace/puppet-blacksmith" should not exist + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: When specifying configurations in managed_modules.yml and using a negative filter - Given a file named "managed_modules.yml" with: + Given a mocked git configuration + And a puppet module "puppet-test" from "fakenamespace" + And a puppet module "puppet-blacksmith" from "fakenamespace" + And a file named "managed_modules.yml" with: """ --- puppet-blacksmith: @@ -717,9 +544,9 @@ Feature: update And a file named "modulesync.yml" with: """ --- - namespace: maestrodev - git_base: https://github.com/ + namespace: fakenamespace """ + And a git_base option appended to "modulesync.yml" for local tests And a file named "config_defaults.yml" with: """ --- @@ -738,21 +565,12 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain "aruba" - And a directory named "modules/maestrodev/puppet-blacksmith" should not exist + And the file named "modules/fakenamespace/puppet-test/test" should contain "aruba" + And a directory named "modules/fakenamespace/puppet-blacksmith" should not exist + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Updating a module with a .sync.yml file - Given a file named "managed_modules.yml" with: - """ - --- - - maestrodev/puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -775,21 +593,7 @@ Feature: update <%= @configs['global-to-overwrite'] %> <%= @configs['module-default'] %> """ - Given I run `git init modules/maestrodev/puppet-test` - Given a file named "modules/maestrodev/puppet-test/.git/config" with: - """ - [core] - repositoryformatversion = 0 - filemode = true - bare = false - logallrefupdates = true - ignorecase = true - precomposeunicode = true - [remote "origin"] - url = https://github.com/maestrodev/puppet-test.git - fetch = +refs/heads/*:refs/remotes/origin/* - """ - Given a file named "modules/maestrodev/puppet-test/.sync.yml" with: + And the puppet module "puppet-test" from "fakenamespace" has a file named ".sync.yml" with: """ --- :global: @@ -804,16 +608,19 @@ Feature: update """ Not managing spec/spec_helper.rb in puppet-test """ - Given I run `cat modules/maestrodev/puppet-test/global-test.md` - Then the output should match: + And the file named "modules/fakenamespace/puppet-test/global-test.md" should contain: """ some-default it-is-overwritten some-value """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Module with custom namespace - Given a file named "managed_modules.yml" with: + Given a mocked git configuration + And a puppet module "puppet-test" from "fakenamespace" + And a puppet module "puppet-lib-file_concat" from "electrical" + And a file named "managed_modules.yml" with: """ --- - puppet-test @@ -822,9 +629,9 @@ Feature: update And a file named "modulesync.yml" with: """ --- - namespace: maestrodev - git_base: https://github.com/ + namespace: fakenamespace """ + And a git_base option appended to "modulesync.yml" for local tests And a file named "config_defaults.yml" with: """ --- @@ -843,23 +650,13 @@ Feature: update Files added: test """ - Given I run `cat modules/maestrodev/puppet-test/.git/config` - Then the output should contain "url = https://github.com/maestrodev/puppet-test.git" - Given I run `cat modules/electrical/puppet-lib-file_concat/.git/config` - Then the output should contain "url = https://github.com/electrical/puppet-lib-file_concat.git" + And the file named "modules/fakenamespace/puppet-test/.git/config" should match /^\s+url = .*fakenamespace.puppet-test$/ + And the file named "modules/electrical/puppet-lib-file_concat/.git/config" should match /^\s+url = .*electrical.puppet-lib-file_concat$/ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" + And the puppet module "puppet-lib-file_concat" from "electrical" should have no commits made by "Aruba" Scenario: Modifying an existing file with values exposed by the module - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -868,7 +665,12 @@ Feature: update And a directory named "moduleroot" And a file named "moduleroot/README.md.erb" with: """ - echo '<%= @configs[:git_base] + @configs[:namespace] %>' + module: <%= @configs[:puppet_module] %> + namespace: <%= @configs[:namespace] %> + """ + And the puppet module "puppet-test" from "fakenamespace" has a file named "README.md" with: + """ + Hello world! """ When I run `msync update --noop` Then the exit status should be 0 @@ -877,15 +679,15 @@ Feature: update Files changed: +diff --git a/README.md b/README.md """ - Given I run `cat modules/maestrodev/puppet-test/README.md` - Then the output should contain: + And the file named "modules/fakenamespace/puppet-test/README.md" should contain: """ - echo 'https://github.com/maestrodev' + module: puppet-test + namespace: fakenamespace """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Running the same update twice and pushing to a remote branch - Given a mocked git configuration - And a remote module repository + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- @@ -899,15 +701,18 @@ Feature: update """ When I run `msync update -m "Update Gemfile" -r test` Then the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have only 1 commit made by "Aruba" + And the puppet module "puppet-test" from "fakenamespace" should have 1 commit made by "Aruba" in branch "test" Given I remove the directory "modules" When I run `msync update -m "Update Gemfile" -r test` Then the exit status should be 0 Then the output should not contain "error" Then the output should not contain "rejected" + And the puppet module "puppet-test" from "fakenamespace" should have only 1 commit made by "Aruba" + And the puppet module "puppet-test" from "fakenamespace" should have 1 commit made by "Aruba" in branch "test" Scenario: Creating a GitHub PR with an update - Given a mocked git configuration - And a remote module repository + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a directory named "moduleroot" And I set the environment variables to: | variable | value | @@ -915,10 +720,10 @@ Feature: update When I run `msync update --noop --branch managed_update --pr` Then the output should contain "Would submit PR " And the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Creating a GitLab MR with an update - Given a mocked git configuration - And a remote module repository + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a directory named "moduleroot" And I set the environment variables to: | variable | value | @@ -926,10 +731,11 @@ Feature: update When I run `msync update --noop --branch managed_update --pr` Then the output should contain "Would submit MR " And the exit status should be 0 + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" Scenario: Repository with a default branch other than master - Given a mocked git configuration - And a remote module repository with "develop" as the default branch + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" + And the puppet module "puppet-test" from "fakenamespace" has the default branch named "develop" And a file named "config_defaults.yml" with: """ --- @@ -943,24 +749,16 @@ Feature: update """ When I run `msync update -m "Update Gemfile"` Then the exit status should be 0 - Then the output should contain "Using repository's default branch: develop" + And the output should contain "Using repository's default branch: develop" + And the puppet module "puppet-test" from "fakenamespace" should have only 1 commit made by "Aruba" + And the puppet module "puppet-test" from "fakenamespace" should have 1 commit made by "Aruba" in branch "develop" - Scenario: Adding a new file from a template using meta data + Scenario: Adding a new file from a template using metadata + Given a basic setup with a puppet module "puppet-test" from "fakenamespace" And a file named "config_defaults.yml" with: """ --- """ - Given a file named "managed_modules.yml" with: - """ - --- - - puppet-test - """ - And a file named "modulesync.yml" with: - """ - --- - namespace: maestrodev - git_base: https://github.com/ - """ And a directory named "moduleroot" And a file named "moduleroot/test.erb" with: """ @@ -970,10 +768,10 @@ Feature: update """ When I run `msync update --noop` Then the exit status should be 0 - Given I run `cat modules/maestrodev/puppet-test/test` - Then the output should contain: + And the file named "modules/fakenamespace/puppet-test/test" should contain: """ module: puppet-test - target: modules/maestrodev/puppet-test/test - workdir: modules/maestrodev/puppet-test + target: modules/fakenamespace/puppet-test/test + workdir: modules/fakenamespace/puppet-test """ + And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba" diff --git a/spec/helpers/faker.rb b/spec/helpers/faker.rb new file mode 100644 index 00000000..7ca66881 --- /dev/null +++ b/spec/helpers/faker.rb @@ -0,0 +1,14 @@ +module ModuleSync + # Faker is a top-level module to keep global faker config + module Faker + def self.working_directory=(path) + @working_directory = path + end + + def self.working_directory + raise 'Working directory must be set' if @working_directory.nil? + FileUtils.mkdir_p @working_directory + @working_directory + end + end +end diff --git a/spec/helpers/faker/puppet_module_remote_repo.rb b/spec/helpers/faker/puppet_module_remote_repo.rb new file mode 100644 index 00000000..07be9617 --- /dev/null +++ b/spec/helpers/faker/puppet_module_remote_repo.rb @@ -0,0 +1,140 @@ +require 'open3' + +require_relative '../faker' + +module ModuleSync + # Fake a remote git repository that holds a puppet module + # + # This module allows to fake a remote repositiory using: + # - a bare repo + # - a temporary cloned repo to operate on the remote before exposing to modulesync + # + # Note: This module needs to have working_directory sets before using it + module Faker + class PuppetModuleRemoteRepo + class CommandExecutionError < StandardError; end + + attr_reader :name, :namespace + + def initialize(name, namespace) + @name = name + @namespace = namespace + end + + def populate + FileUtils.chdir(Faker.working_directory) do + run %W[git init --bare #{bare_repo_dir}] + run %W[git clone #{bare_repo_dir} #{tmp_repo_dir}] + + module_short_name = name.split('-').last + + FileUtils.chdir(tmp_repo_dir) do + metadata = { + name: "modulesync-#{module_short_name}", + version: '0.4.2', + author: 'ModuleSync team', + } + + File.write 'metadata.json', metadata.to_json + run %w[git add metadata.json] + run %w[git commit --message] << 'Initial commit' + run %w[git push] + end + end + end + + def read_only=(value) + mode = value ? '0444' : '0644' + FileUtils.chdir(bare_repo_dir) do + run %W[git config core.sharedRepository #{mode}] + end + end + + def default_branch + FileUtils.chdir(bare_repo_dir) do + stdout = run %w[git symbolic-ref --short HEAD] + return stdout.chomp + end + end + + def default_branch=(value) + FileUtils.chdir(bare_repo_dir) do + run %W[git branch -M #{default_branch} #{value}] + run %W[git symbolic-ref HEAD refs/heads/#{value}] + end + end + + def read_file(filename, branch = nil) + branch ||= default_branch + FileUtils.chdir(bare_repo_dir) do + return run %W[git show #{branch}:#{filename}] + rescue CommandExecutionError + return nil + end + end + + def add_file(filename, content, branch = nil) + branch ||= default_branch + FileUtils.chdir(tmp_repo_dir) do + run %W[git checkout #{branch}] + File.write filename, content + run %W[git add #{filename}] + run %w[git commit --message] << "Add file: '#{filename}'" + run %w[git push] + end + end + + def commit_count_between(commit1, commit2) + FileUtils.chdir(bare_repo_dir) do + stdout = run %W[git rev-list --count #{commit1}..#{commit2}] + return Integer(stdout) + end + end + + def commit_count_by(author, commit = nil) + FileUtils.chdir(bare_repo_dir) do + commit ||= '--all' + stdout = run %W[git rev-list #{commit} --author #{author} --count] + return Integer(stdout) + end + end + + def remote_url + "file://#{bare_repo_dir}" + end + + def self.git_base + "file://#{Faker.working_directory}/bare/" + end + + private + + def tmp_repo_dir + File.join Faker.working_directory, 'tmp', namespace, name + end + + def bare_repo_dir + File.join Faker.working_directory, 'bare', namespace, "#{name}.git" + end + + GIT_ENV = { + 'GIT_AUTHOR_NAME' => 'Faker', + 'GIT_AUTHOR_EMAIL' => 'faker@example.com', + 'GIT_COMMITTER_NAME' => 'Faker', + 'GIT_COMMITTER_EMAIL' => 'faker@example.com', + }.freeze + + def run(command) + stdout, stderr, status = Open3.capture3(GIT_ENV, *command) + return stdout if status.success? + + warn "Command '#{command}' failed: #{status}" + warn ' STDOUT:' + warn stdout + warn ' STDERR:' + warn stderr + raise CommandExecutionError, "Command '#{command}' failed: #{status}" + end + end + end +end