github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/wrappers/icons_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package wrappers_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  	"sort"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/dirs"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/snap/snaptest"
    33  	"github.com/snapcore/snapd/testutil"
    34  	"github.com/snapcore/snapd/wrappers"
    35  )
    36  
    37  type iconsTestSuite struct {
    38  	testutil.BaseTest
    39  	tempdir string
    40  }
    41  
    42  var _ = Suite(&iconsTestSuite{})
    43  
    44  func (s *iconsTestSuite) SetUpTest(c *C) {
    45  	s.BaseTest.SetUpTest(c)
    46  	s.BaseTest.AddCleanup(snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}))
    47  	s.tempdir = c.MkDir()
    48  	dirs.SetRootDir(s.tempdir)
    49  }
    50  
    51  func (s *iconsTestSuite) TearDownTest(c *C) {
    52  	dirs.SetRootDir("")
    53  	s.BaseTest.TearDownTest(c)
    54  }
    55  
    56  func (s *iconsTestSuite) TestFindIconFiles(c *C) {
    57  	info := snaptest.MockSnap(c, packageHello, &snap.SideInfo{Revision: snap.R(11)})
    58  
    59  	baseDir := info.MountDir()
    60  	iconsDir := filepath.Join(baseDir, "meta", "gui", "icons")
    61  	c.Assert(os.MkdirAll(filepath.Join(iconsDir, "hicolor", "256x256", "apps"), 0755), IsNil)
    62  	c.Assert(os.MkdirAll(filepath.Join(iconsDir, "hicolor", "scalable", "apps"), 0755), IsNil)
    63  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "hicolor", "256x256", "apps", "snap.hello-snap.foo.png"), []byte("256x256"), 0644), IsNil)
    64  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "hicolor", "scalable", "apps", "snap.hello-snap.foo.svg"), []byte("scalable"), 0644), IsNil)
    65  
    66  	// Some files that shouldn't be picked up
    67  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "hicolor", "scalable", "apps", "snap.hello-snap.foo.exe"), []byte("bad extension"), 0644), IsNil)
    68  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "hicolor", "scalable", "apps", "org.example.hello.png"), []byte("bad prefix"), 0644), IsNil)
    69  	c.Assert(os.MkdirAll(filepath.Join(iconsDir, "snap.whatever"), 0755), IsNil)
    70  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "snap.whatever", "snap.hello-snap.foo.png"), []byte("bad dir"), 0644), IsNil)
    71  
    72  	icons, err := wrappers.FindIconFiles(info.SnapName(), iconsDir)
    73  	sort.Strings(icons)
    74  	c.Assert(err, IsNil)
    75  	c.Check(icons, DeepEquals, []string{
    76  		"hicolor/256x256/apps/snap.hello-snap.foo.png",
    77  		"hicolor/scalable/apps/snap.hello-snap.foo.svg",
    78  	})
    79  }
    80  
    81  func (s *iconsTestSuite) TestAddSnapIcons(c *C) {
    82  	info := snaptest.MockSnap(c, packageHello, &snap.SideInfo{Revision: snap.R(11)})
    83  
    84  	baseDir := info.MountDir()
    85  	iconsDir := filepath.Join(baseDir, "meta", "gui", "icons")
    86  	c.Assert(os.MkdirAll(filepath.Join(iconsDir, "hicolor", "scalable", "apps"), 0755), IsNil)
    87  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "hicolor", "scalable", "apps", "snap.hello-snap.foo.svg"), []byte("scalable"), 0644), IsNil)
    88  
    89  	c.Assert(wrappers.AddSnapIcons(info), IsNil)
    90  	iconFile := filepath.Join(dirs.SnapDesktopIconsDir, "hicolor", "scalable", "apps", "snap.hello-snap.foo.svg")
    91  	c.Check(iconFile, testutil.FileEquals, "scalable")
    92  }
    93  
    94  func (s *iconsTestSuite) TestRemoveSnapIcons(c *C) {
    95  	iconDir := filepath.Join(dirs.SnapDesktopIconsDir, "hicolor", "scalable", "apps")
    96  	iconFile := filepath.Join(iconDir, "snap.hello-snap.foo.svg")
    97  	c.Assert(os.MkdirAll(iconDir, 0755), IsNil)
    98  	c.Assert(ioutil.WriteFile(iconFile, []byte("contents"), 0644), IsNil)
    99  
   100  	info := snaptest.MockSnap(c, packageHello, &snap.SideInfo{Revision: snap.R(11)})
   101  	c.Assert(wrappers.RemoveSnapIcons(info), IsNil)
   102  	c.Check(iconFile, testutil.FileAbsent)
   103  }
   104  
   105  func (s *iconsTestSuite) TestParallelInstanceAddIcons(c *C) {
   106  	info := snaptest.MockSnap(c, packageHello, &snap.SideInfo{Revision: snap.R(11)})
   107  	info.InstanceKey = "instance"
   108  
   109  	baseDir := info.MountDir()
   110  	iconsDir := filepath.Join(baseDir, "meta", "gui", "icons")
   111  	c.Assert(os.MkdirAll(filepath.Join(iconsDir, "hicolor", "scalable", "apps"), 0755), IsNil)
   112  	c.Assert(ioutil.WriteFile(filepath.Join(iconsDir, "hicolor", "scalable", "apps", "snap.hello-snap.foo.svg"), []byte("scalable"), 0644), IsNil)
   113  
   114  	c.Assert(wrappers.AddSnapIcons(info), IsNil)
   115  	iconFile := filepath.Join(dirs.SnapDesktopIconsDir, "hicolor", "scalable", "apps", "snap.hello-snap_instance.foo.svg")
   116  	c.Check(iconFile, testutil.FileEquals, "scalable")
   117  
   118  	// No file installed without the instance qualifier
   119  	iconFile = filepath.Join(dirs.SnapDesktopIconsDir, "hicolor", "scalable", "apps", "snap.hello-snap.foo.svg")
   120  	c.Check(iconFile, testutil.FileAbsent)
   121  }
   122  
   123  func (s *iconsTestSuite) TestParallelInstanceRemoveIcons(c *C) {
   124  	iconDir := filepath.Join(dirs.SnapDesktopIconsDir, "hicolor", "scalable", "apps")
   125  	c.Assert(os.MkdirAll(iconDir, 0755), IsNil)
   126  	snapNameFile := filepath.Join(iconDir, "snap.hello-snap.foo.svg")
   127  	c.Assert(ioutil.WriteFile(snapNameFile, []byte("contents"), 0644), IsNil)
   128  	instanceNameFile := filepath.Join(iconDir, "snap.hello-snap_instance.foo.svg")
   129  	c.Assert(ioutil.WriteFile(instanceNameFile, []byte("contents"), 0644), IsNil)
   130  
   131  	info := snaptest.MockSnap(c, packageHello, &snap.SideInfo{Revision: snap.R(11)})
   132  	info.InstanceKey = "instance"
   133  	c.Assert(wrappers.RemoveSnapIcons(info), IsNil)
   134  	c.Check(instanceNameFile, testutil.FileAbsent)
   135  	// The non-instance qualified icon remains
   136  	c.Check(snapNameFile, testutil.FilePresent)
   137  }