-
Notifications
You must be signed in to change notification settings - Fork 12
/
snaps_test.go
90 lines (78 loc) · 2.08 KB
/
snaps_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (C) 2020-2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snaps
import (
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type snapsTestSuite struct{}
var _ = Suite(&snapsTestSuite{})
func (s *snapsTestSuite) SetUpTest(c *C) {}
func (s *snapsTestSuite) TestRevision(c *C) {
tmpDir := c.MkDir()
r := MockSnapRoot(tmpDir)
defer r()
tt := []struct {
snap string
nomkdir bool
revision string
expected string
expectedErrPattern string
}{
{
snap: "test-snap",
revision: "294",
expected: "294",
},
{
snap: "test-not-a-snap",
nomkdir: true,
expectedErrPattern: "no such snap: test-not-a-snap",
},
{
snap: "test-not-a-snap",
expectedErrPattern: "readlink .*: no such file or directory",
},
}
for _, t := range tt {
snapDir := filepath.Join(tmpDir, t.snap)
currentSymlink := filepath.Join(snapDir, "current")
// mock the snap dir
if t.snap != "" && !t.nomkdir {
c.Assert(os.MkdirAll(snapDir, 0777), IsNil)
}
// mock the symlink
if t.revision != "" {
// make the symlink
c.Assert(os.Symlink(t.revision, currentSymlink), IsNil)
}
rev, err := Revision(t.snap)
if t.expectedErrPattern != "" {
c.Assert(err, ErrorMatches, t.expectedErrPattern)
} else {
c.Assert(err, IsNil)
c.Assert(rev, Equals, t.expected)
}
// un-mock the symlink
if t.revision != "" {
c.Assert(os.Remove(currentSymlink), IsNil)
}
}
}