github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/unlink_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 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 osutil_test 21 22 import ( 23 "os" 24 "path/filepath" 25 "sort" 26 27 "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/osutil" 30 ) 31 32 type unlinkSuite struct { 33 d string 34 } 35 36 var _ = check.Suite(&unlinkSuite{}) 37 38 func (s *unlinkSuite) checkDirnames(c *check.C, names []string) { 39 dir, err := os.Open(s.d) 40 c.Assert(err, check.IsNil) 41 defer dir.Close() 42 found, err := dir.Readdirnames(-1) 43 c.Assert(err, check.IsNil) 44 sort.Strings(names) 45 sort.Strings(found) 46 c.Check(found, check.DeepEquals, names) 47 } 48 49 func (s *unlinkSuite) SetUpTest(c *check.C) { 50 s.d = c.MkDir() 51 s.mkFixture(c) 52 } 53 54 func (s *unlinkSuite) mkFixture(c *check.C) { 55 for _, fname := range []string{"foo", "bar", "baz", "quux"} { 56 f, err := os.Create(filepath.Join(s.d, fname)) 57 if err == nil { 58 f.Close() 59 } else if !os.IsExist(err) { 60 c.Fatal(err) 61 } 62 } 63 if err := os.Mkdir(filepath.Join(s.d, "dir"), 0700); err != nil && !os.IsExist(err) { 64 c.Fatal(err) 65 } 66 } 67 68 func (s *unlinkSuite) TestUnlinkMany(c *check.C) { 69 c.Assert(osutil.UnlinkMany(s.d, []string{"bar", "does-not-exist", "baz"}), check.IsNil) 70 71 s.checkDirnames(c, []string{"foo", "quux", "dir"}) 72 } 73 74 func (s *unlinkSuite) TestUnlinkManyAt(c *check.C) { 75 d, err := os.Open(s.d) 76 c.Assert(err, check.IsNil) 77 c.Assert(osutil.UnlinkManyAt(d, []string{"bar", "does-not-exist", "baz"}), check.IsNil) 78 79 s.checkDirnames(c, []string{"foo", "quux", "dir"}) 80 } 81 82 func (s *unlinkSuite) TestUnlinkManyFails(c *check.C) { 83 type T struct { 84 dirname string 85 filenames []string 86 expected string 87 } 88 tests := []T{ 89 { 90 dirname: filepath.Join(s.d, "does-not-exist"), 91 filenames: []string{"bar", "baz"}, 92 expected: `open /tmp/.*/does-not-exist: no such file or directory`, 93 }, { 94 dirname: filepath.Join(s.d, "foo"), 95 filenames: []string{"bar", "baz"}, 96 expected: `open /tmp/.*/foo: not a directory`, 97 }, { 98 dirname: s.d, 99 filenames: []string{"bar", "dir", "baz"}, 100 expected: `remove dir: is a directory`, 101 }, 102 } 103 104 for i, test := range tests { 105 c.Check(osutil.UnlinkMany(test.dirname, test.filenames), check.ErrorMatches, test.expected, check.Commentf("%d", i)) 106 s.mkFixture(c) 107 } 108 }