Support Mercurial tags, restructure examples
[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).at_least_once
74       Dir.expects(:chdir).with(@path).yields
75     end
76     context "when given a non-SHA as the resource revision" do
77       before do
78         @provider.expects(:hg).with('parents').returns(fixture(:hg_parents))
79       end
80       context "when its SHA is not different than the current SHA" do
81         before do
82           @resource.expects(:value).with(:revision).returns('0.6').at_least_once
83         end
84         it "should return the ref" do
85           @provider.expects(:hg).with('tags').returns(fixture(:hg_tags))
86           @provider.revision.should == '0.6'
87         end
88       end
89       context "when its SHA is different than the current SHA" do
90         before do
91           @resource.expects(:value).with(:revision).returns('0.5.3').at_least_once
92         end
93         it "should return the current SHA" do
94           @provider.expects(:hg).with('tags').returns(fixture(:hg_tags))
95           @provider.revision.should == '34e6012c783a'
96         end          
97       end
98     end
99     context "when given a SHA as the resource revision" do
100       before do
101         @provider.expects(:hg).with('parents').returns(fixture(:hg_parents))
102       end
103       context "when it is the same as the current SHA" do
104         before do
105           @resource.expects(:value).with(:revision).returns('34e6012c783a').at_least_once
106         end
107         it "should return it" do
108           @provider.expects(:hg).with('tags').never
109           @provider.revision.should == '34e6012c783a'
110         end
111       end
112       context "when it is not the same as the current SHA" do
113         before do
114           @resource.expects(:value).with(:revision).returns('34e6012c7').at_least_once
115         end
116         it "should return the current SHA" do
117           @provider.expects(:hg).with('tags').returns(fixture(:hg_tags))          
118           @provider.revision.should == '34e6012c783a'
119         end
120       end
121     end
122   end
123   
124   describe "when setting the revision property" do
125     before do
126       @resource.expects(:value).with(:path).returns(@path).at_least_once
127       @revision = '6aa99e9b3ab1'
128     end
129     it "should use 'hg update ---clean -r'" do
130       Dir.expects(:chdir).with(@path).yields
131       @provider.expects('hg').with('pull')
132       @provider.expects('hg').with('merge')
133       @provider.expects('hg').with('update', '--clean', '-r', @revision)
134       @provider.revision = @revision
135     end
136   end
137
138 end