github.com/atlassian/git-lob@v0.0.0-20150806085256-2386a5ed291a/core/links_test.go (about)

     1  package core
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/atlassian/git-lob/Godeps/_workspace/src/github.com/onsi/ginkgo"
     9  	. "github.com/atlassian/git-lob/Godeps/_workspace/src/github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Links", func() {
    13  	var target string
    14  	var links []string
    15  	BeforeEach(func() {
    16  		tmp := os.TempDir()
    17  		target = filepath.Join(tmp, "linktarget.txt")
    18  		links = make([]string, 0, 5)
    19  		links = append(links, filepath.Join(tmp, "link1.txt"))
    20  		links = append(links, filepath.Join(tmp, "link2.txt"))
    21  		links = append(links, filepath.Join(tmp, "link3.txt"))
    22  		links = append(links, filepath.Join(tmp, "link4.txt"))
    23  		links = append(links, filepath.Join(tmp, "link5.txt"))
    24  
    25  		ioutil.WriteFile(target, []byte("Something something"), 0644)
    26  	})
    27  
    28  	AfterEach(func() {
    29  		os.Remove(target)
    30  		for _, f := range links {
    31  			os.Remove(f)
    32  		}
    33  
    34  	})
    35  
    36  	It("creates and counts hard links", func() {
    37  		var err error
    38  		var lc int
    39  
    40  		lc, err = GetHardLinkCount(target)
    41  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    42  		Expect(lc).To(BeEquivalentTo(1), "Hard link count should be 1 to start with")
    43  
    44  		err = CreateHardLink(target, links[0])
    45  		Expect(err).To(BeNil(), "CreateHardLink should not fail")
    46  		lc, err = GetHardLinkCount(target)
    47  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    48  		Expect(lc).To(BeEquivalentTo(2), "Hard link count incorrect")
    49  
    50  		err = CreateHardLink(target, links[1])
    51  		Expect(err).To(BeNil(), "CreateHardLink should not fail")
    52  		lc, err = GetHardLinkCount(target)
    53  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    54  		Expect(lc).To(BeEquivalentTo(3), "Hard link count incorrect")
    55  
    56  		err = CreateHardLink(target, links[2])
    57  		Expect(err).To(BeNil(), "CreateHardLink should not fail")
    58  		lc, err = GetHardLinkCount(target)
    59  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    60  		Expect(lc).To(BeEquivalentTo(4), "Hard link count incorrect")
    61  
    62  		err = CreateHardLink(target, links[3])
    63  		Expect(err).To(BeNil(), "CreateHardLink should not fail")
    64  		lc, err = GetHardLinkCount(target)
    65  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    66  		Expect(lc).To(BeEquivalentTo(5), "Hard link count incorrect")
    67  
    68  		err = CreateHardLink(target, links[4])
    69  		Expect(err).To(BeNil(), "CreateHardLink should not fail")
    70  		lc, err = GetHardLinkCount(target)
    71  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    72  		Expect(lc).To(BeEquivalentTo(6), "Hard link count incorrect")
    73  
    74  		os.Remove(links[2])
    75  		os.Remove(links[4])
    76  		lc, err = GetHardLinkCount(target)
    77  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    78  		Expect(lc).To(BeEquivalentTo(4), "Hard link count incorrect after removal")
    79  
    80  		os.Remove(target)
    81  		lc, err = GetHardLinkCount(links[0])
    82  		Expect(err).To(BeNil(), "GetHardLinkCount should not fail")
    83  		Expect(lc).To(BeEquivalentTo(3), "Hard link count incorrect after removal of target")
    84  
    85  	})
    86  })