|
| 1 | +require 'spec_helper' |
| 2 | +require 'puppetfiles/bin/update_module' |
| 3 | + |
| 4 | +class UpdateModule |
| 5 | + include Puppetfiles::Bin::UpdateModule |
| 6 | + attr_accessor :config, :argv, :status, :output |
| 7 | + def initialize(argv, config) |
| 8 | + @argv = argv |
| 9 | + @config = config |
| 10 | + @output = '' |
| 11 | + end |
| 12 | + |
| 13 | + def output(msg = nil) |
| 14 | + return @output unless msg |
| 15 | + @output << msg |
| 16 | + nil |
| 17 | + end |
| 18 | + |
| 19 | + { ok: 0, warning: 1, critical: 2, unknown: 3 }.each do |name, code| |
| 20 | + define_method(name) do |*args| |
| 21 | + @status = code |
| 22 | + output(*args) |
| 23 | + exit(code) |
| 24 | + end |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +describe Puppetfiles::Bin::UpdateModule do |
| 29 | + let(:puppetfile) { Tempfile.new('Puppetfile.update') } |
| 30 | + let(:mod1) { { name: 'mod1', version: '1.0.0', options: {} } } |
| 31 | + let(:mod2) do |
| 32 | + { name: 'mod2', |
| 33 | + version: '', |
| 34 | + options: { git: '[email protected]:user/repo.git', ref: '1.0.0' } } |
| 35 | + end |
| 36 | + let(:value) { 'whatever' } |
| 37 | + |
| 38 | + before(:example) do |
| 39 | + ::Puppetfiles.dump([{ path: puppetfile.path, modules: [mod1, mod2] }]) |
| 40 | + end |
| 41 | + after(:example) { puppetfile.close! } |
| 42 | + |
| 43 | + context 'with `:version` set' do |
| 44 | + let(:script) do |
| 45 | + UpdateModule.new([mod1[:name], puppetfile.path], version: value) |
| 46 | + end |
| 47 | + it 'should update module version' do |
| 48 | + expect { script.run }.to raise_error(SystemExit) |
| 49 | + expect(script.status).to eq(0) |
| 50 | + loaded = ::Puppetfiles.load [puppetfile.path] |
| 51 | + found = loaded.first[:modules].find { |m| m[:name] == mod1[:name] } |
| 52 | + expect(found).to be_truthy |
| 53 | + expect(found[:version]).to eq(value) |
| 54 | + end |
| 55 | + end |
| 56 | + [:git, :ref].each do |name| |
| 57 | + context "with option `:#{name}` set" do |
| 58 | + let(:script) do |
| 59 | + UpdateModule.new([mod2[:name], puppetfile.path], name => value) |
| 60 | + end |
| 61 | + it "should update module option `:#{name}`" do |
| 62 | + expect { script.run }.to raise_error(SystemExit) |
| 63 | + expect(script.status).to eq(0) |
| 64 | + loaded = ::Puppetfiles.load [puppetfile.path] |
| 65 | + found = loaded.first[:modules].find { |m| m[:name] == mod2[:name] } |
| 66 | + expect(found).to be_truthy |
| 67 | + expect(found[:options][name]).to eq(value) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments