~stephen-stewart/snapweb/client-side-filtering-and-ordering

« back to all changes in this revision

Viewing changes to snappy/icon_test.go

  • Committer: Snappy Tarmac
  • Author(s): Sergio Schvezov
  • Date: 2015-04-29 14:50:53 UTC
  • mfrom: (84.6.13 icons)
  • Revision ID: snappy_tarmac-20150429145053-olbqcqlo2ra5aesu
Adding icon support by sergiusens approved by sergiusens

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package snappy
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "io/ioutil"
 
6
        "os"
 
7
        "path/filepath"
 
8
 
 
9
        . "gopkg.in/check.v1"
 
10
)
 
11
 
 
12
type IconSuite struct {
 
13
        dataPath string
 
14
}
 
15
 
 
16
var _ = Suite(&IconSuite{})
 
17
 
 
18
func (s *IconSuite) SetUpTest(c *C) {
 
19
        s.dataPath = c.MkDir()
 
20
        os.Setenv("SNAP_APP_DATA_PATH", s.dataPath)
 
21
}
 
22
 
 
23
func (s *IconSuite) TestIconDir(c *C) {
 
24
        iconDir, relativePath, err := IconDir()
 
25
        c.Assert(err, IsNil)
 
26
        c.Check(iconDir, Equals, filepath.Join(s.dataPath, "icons"))
 
27
        c.Check(relativePath, Equals, "icons")
 
28
}
 
29
 
 
30
func (s *IconSuite) TestNoSnapAppDataPathCausesError(c *C) {
 
31
        os.Setenv("SNAP_APP_DATA_PATH", "")
 
32
        _, _, err := IconDir()
 
33
        c.Assert(err, Equals, ErrDataPathNotSet)
 
34
}
 
35
 
 
36
func (s *IconSuite) TestIconDirCreateFails(c *C) {
 
37
        fileAsDir := filepath.Join(s.dataPath, "badDataPath")
 
38
        c.Assert(ioutil.WriteFile(fileAsDir, []byte{}, 0644), IsNil)
 
39
        os.Setenv("SNAP_APP_DATA_PATH", fileAsDir)
 
40
        _, _, err := IconDir()
 
41
        c.Assert(err, Equals, ErrOnIconDataPathSet)
 
42
}
 
43
 
 
44
type IconPathSuite struct {
 
45
        dataPath    string
 
46
        pkgIconPath string
 
47
}
 
48
 
 
49
var _ = Suite(&IconPathSuite{})
 
50
 
 
51
func (s *IconPathSuite) SetUpTest(c *C) {
 
52
        s.dataPath = c.MkDir()
 
53
        os.Setenv("SNAP_APP_DATA_PATH", s.dataPath)
 
54
 
 
55
        s.pkgIconPath = filepath.Join(c.MkDir(), "pkgIcon.png")
 
56
        c.Assert(ioutil.WriteFile(s.pkgIconPath, []byte("png"), 07555), IsNil)
 
57
}
 
58
 
 
59
func (s *IconPathSuite) TestIconCopy(c *C) {
 
60
        relativePath, err := localIconPath("mypackage.sergiusens", s.pkgIconPath)
 
61
        c.Assert(err, IsNil)
 
62
        iconBaseName := fmt.Sprintf("icons/mypackage.sergiusens_%s", filepath.Base(s.pkgIconPath))
 
63
        c.Check(relativePath, Equals, filepath.Join("/", iconBaseName))
 
64
 
 
65
        contents, err := ioutil.ReadFile(filepath.Join(s.dataPath, iconBaseName))
 
66
        c.Assert(err, IsNil)
 
67
 
 
68
        c.Assert(string(contents), Equals, "png")
 
69
}
 
70
 
 
71
func (s *IconPathSuite) TestIconCopyNoDataPath(c *C) {
 
72
        os.Setenv("SNAP_APP_DATA_PATH", "")
 
73
        _, err := localIconPath("mypackage.sergiusens", s.pkgIconPath)
 
74
        c.Assert(err, Equals, ErrDataPathNotSet)
 
75
}
 
76
 
 
77
func (s *IconPathSuite) TestIconCopyNoIcon(c *C) {
 
78
        _, err := localIconPath("mypackage.sergiusens", "somerandompath")
 
79
        c.Assert(err, Equals, ErrIconNotExist)
 
80
}
 
81
 
 
82
func (s *IconPathSuite) TestIconCopyTargetIconExists(c *C) {
 
83
        iconBaseName := fmt.Sprintf("icons/mypackage.sergiusens_%s", filepath.Base(s.pkgIconPath))
 
84
        c.Assert(os.MkdirAll(filepath.Join(s.dataPath, "icons"), 0755), IsNil)
 
85
        c.Assert(ioutil.WriteFile(filepath.Join(s.dataPath, iconBaseName), []byte{}, 0644), IsNil)
 
86
 
 
87
        relativePath, err := localIconPath("mypackage.sergiusens", s.pkgIconPath)
 
88
        c.Assert(err, IsNil)
 
89
        c.Check(relativePath, Equals, filepath.Join("/", iconBaseName))
 
90
}