Basic Mercurial support
[puppet_vcsrepo.git] / spec / unit / puppet / provider / vcsrepo / hg_spec.rb
1 require 'pathname'; Pathname.new(__FILE__).realpath.ascend { |x| begin; require (x + 'spec_helper.rb'); break; rescue LoadError; end }
2
3 provider_class = Puppet::Type.type(:vcsrepo).provider(:hg)
4
5 describe provider_class do
6
7   before :each do
8     @resource = stub("resource")
9     @provider = provider_class.new(@resource)
10     @path = '/tmp/vcsrepo'
11   end
12
13   describe 'when creating' do
14     before do
15       @resource.expects(:value).with(:path).returns(@path).at_least_once
16     end
17     context "when a source is given" do
18       before do
19         @source = 'http://example.com/hg/repo'
20         @resource.expects(:value).with(:source).returns(@source).at_least_once
21       end
22       context "and when a revision is given" do
23         before do
24           @revision = '6aa99e9b3ac2'
25           @resource.expects(:value).with(:revision).returns(@revision).at_least_once
26         end
27         it "should execute 'hg clone -u' with the revision" do
28           @provider.expects(:hg).with('clone', '-u', @revision, @source, @path)
29           @provider.create
30         end        
31       end
32       context "and when a revision is not given" do
33         before do
34           @resource.expects(:value).with(:revision).returns(nil).at_least_once
35         end
36         it "should just execute 'hg clone' without a revision" do
37           @provider.expects(:hg).with('clone', @source, @path)
38           @provider.create
39         end        
40       end
41     end
42     context "when a source is not given" do
43       before do
44         @resource.expects(:value).with(:source).returns(nil).at_least_once
45       end
46       it "should execute 'hg init'" do
47         @provider.expects(:hg).with('init', @path)
48         @provider.create
49       end
50     end
51   end
52
53   describe 'when destroying' do
54     it "it should remove the directory" do
55       @resource.expects(:value).with(:path).returns(@path).at_least_once
56       FileUtils.expects(:rm_rf).with(@path)
57       @provider.destroy
58     end
59   end
60
61   describe "when checking existence" do
62     before do
63       @resource.expects(:value).with(:path).returns(@path)
64     end
65     it "should check for the directory" do
66       File.expects(:directory?).with(File.join(@path, '.hg'))
67       @provider.exists?
68     end
69   end
70
71   describe "when checking the revision property" do
72     before do
73       @resource.expects(:value).with(:path).returns(@path)
74     end
75     it "should use 'hg tip'" do
76       @provider.expects('hg').with('parents').returns(fixture(:hg_parents))
77       Dir.expects(:chdir).with(@path).yields
78       @provider.revision.should == '34e6012c783a'
79     end
80   end
81   
82   describe "when setting the revision property" do
83     before do
84       @resource.expects(:value).with(:path).returns(@path).at_least_once
85       @revision = '6aa99e9b3ab1'
86     end
87     it "should use 'hg update ---clean -r'" do
88       Dir.expects(:chdir).with(@path).yields
89       @provider.expects('hg').with('pull')
90       @provider.expects('hg').with('merge')
91       @provider.expects('hg').with('update', '--clean', '-r', @revision)
92       @provider.revision = @revision
93     end
94   end
95
96 end