File: copy_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,748 kB
  • sloc: makefile: 20
file content (88 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download | duplicates (2)
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
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package fs_test

import (
	"path/filepath"
	"testing"

	ft "github.com/juju/testing/filetesting"
	gc "gopkg.in/check.v1"

	"github.com/juju/utils/fs"
)

type copySuite struct{}

var _ = gc.Suite(&copySuite{})

func TestPackage(t *testing.T) {
	gc.TestingT(t)
}

var copyTests = []struct {
	about string
	src   ft.Entries
	dst   ft.Entries
	err   string
}{{
	about: "one file",
	src: []ft.Entry{
		ft.File{"file", "data", 0756},
	},
}, {
	about: "one directory",
	src: []ft.Entry{
		ft.Dir{"dir", 0777},
	},
}, {
	about: "one symlink",
	src: []ft.Entry{
		ft.Symlink{"link", "/foo"},
	},
}, {
	about: "several entries",
	src: []ft.Entry{
		ft.Dir{"top", 0755},
		ft.File{"top/foo", "foodata", 0644},
		ft.File{"top/bar", "bardata", 0633},
		ft.Dir{"top/next", 0721},
		ft.Symlink{"top/next/link", "../foo"},
		ft.File{"top/next/another", "anotherdata", 0644},
	},
}, {
	about: "destination already exists",
	src: []ft.Entry{
		ft.Dir{"dir", 0777},
	},
	dst: []ft.Entry{
		ft.Dir{"dir", 0777},
	},
	err: `will not overwrite ".+dir"`,
}, {
	about: "source with unwritable directory",
	src: []ft.Entry{
		ft.Dir{"dir", 0555},
	},
}}

func (*copySuite) TestCopy(c *gc.C) {
	for i, test := range copyTests {
		c.Logf("test %d: %v", i, test.about)
		src, dst := c.MkDir(), c.MkDir()
		test.src.Create(c, src)
		test.dst.Create(c, dst)
		path := test.src[0].GetPath()
		err := fs.Copy(
			filepath.Join(src, path),
			filepath.Join(dst, path),
		)
		if test.err != "" {
			c.Check(err, gc.ErrorMatches, test.err)
		} else {
			c.Assert(err, gc.IsNil)
			test.src.Check(c, dst)
		}
	}
}