2020-01-16 22:53:48 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("ChangeDetector", func() {
|
|
|
|
var testFolder string
|
|
|
|
var scanner *ChangeDetector
|
|
|
|
|
2020-01-18 04:41:10 +01:00
|
|
|
lastModifiedSince := time.Time{}
|
|
|
|
|
2020-01-16 22:53:48 +01:00
|
|
|
BeforeEach(func() {
|
|
|
|
testFolder, _ = ioutil.TempDir("", "cloudsonic_tests")
|
|
|
|
err := os.MkdirAll(testFolder, 0700)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
scanner = NewChangeDetector(testFolder)
|
2020-01-16 22:53:48 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
It("detects changes recursively", func() {
|
|
|
|
// Scan empty folder
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err := scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(ConsistOf("."))
|
|
|
|
|
|
|
|
// Add one subfolder
|
|
|
|
err = os.MkdirAll(path.Join(testFolder, "a"), 0700)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(ConsistOf(".", "/a"))
|
|
|
|
|
|
|
|
// Add more subfolders
|
|
|
|
err = os.MkdirAll(path.Join(testFolder, "a", "b", "c"), 0700)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(ConsistOf("/a", "/a/b", "/a/b/c"))
|
|
|
|
|
|
|
|
// Scan with no changes
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(BeEmpty())
|
|
|
|
|
|
|
|
// New file in subfolder
|
|
|
|
_, err = os.Create(path.Join(testFolder, "a", "b", "empty.txt"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(ConsistOf("/a/b"))
|
|
|
|
|
|
|
|
// Delete file in subfolder
|
|
|
|
err = os.Remove(path.Join(testFolder, "a", "b", "empty.txt"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(ConsistOf("/a/b"))
|
|
|
|
|
|
|
|
// Delete subfolder
|
|
|
|
err = os.Remove(path.Join(testFolder, "a", "b", "c"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(ConsistOf("/a/b/c"))
|
|
|
|
Expect(changed).To(ConsistOf("/a/b"))
|
|
|
|
|
2020-01-18 04:41:10 +01:00
|
|
|
// Only returns changes after lastModifiedSince
|
|
|
|
lastModifiedSince = time.Now()
|
|
|
|
newScanner := NewChangeDetector(testFolder)
|
|
|
|
changed, deleted, err = newScanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(BeEmpty())
|
|
|
|
Expect(changed).To(BeEmpty())
|
|
|
|
|
|
|
|
_, err = os.Create(path.Join(testFolder, "a", "b", "new.txt"))
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = newScanner.Scan(lastModifiedSince)
|
2020-01-16 22:53:48 +01:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(deleted).To(BeEmpty())
|
|
|
|
Expect(changed).To(ConsistOf("/a/b"))
|
|
|
|
})
|
|
|
|
})
|