lib/versioner: Purge the empty directories in .stversions (fixes #4406)
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4514 LGTM: AudriusButkevicius, imsodin
This commit is contained in:
committed by
Audrius Butkevicius
parent
0518a92cdb
commit
9471b9f6af
75
lib/versioner/empty_dir_tracker_test.go
Normal file
75
lib/versioner/empty_dir_tracker_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright (C) 2017 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package versioner
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/d4l3k/messagediff"
|
||||
)
|
||||
|
||||
// TestEmptyDirs models the following .stversions structure:
|
||||
// .stversions/
|
||||
// ├── keep1
|
||||
// │ └── file1
|
||||
// ├── keep2
|
||||
// │ └── keep21
|
||||
// │ └── keep22
|
||||
// │ └── file1
|
||||
// ├── remove1
|
||||
// └── remove2
|
||||
// └── remove21
|
||||
// └── remove22
|
||||
func TestEmptyDirs(t *testing.T) {
|
||||
var paths = []struct {
|
||||
path string
|
||||
isFile bool
|
||||
}{
|
||||
{".", false},
|
||||
{"keep1", false},
|
||||
{"keep1/file1", true},
|
||||
{"keep2", false},
|
||||
{"keep2/keep21", false},
|
||||
{"keep2/keep21/keep22", false},
|
||||
{"keep2/keep21/keep22/file1", true},
|
||||
{"remove1", false},
|
||||
{"remove2", false},
|
||||
{"remove2/remove21", false},
|
||||
{"remove2/remove21/remove22", false},
|
||||
}
|
||||
|
||||
var expected = []string{
|
||||
"remove2/remove21/remove22",
|
||||
"remove2/remove21",
|
||||
"remove2",
|
||||
"remove1",
|
||||
}
|
||||
|
||||
// For compatibility with Windows
|
||||
for i, p := range paths {
|
||||
paths[i].path = filepath.FromSlash(p.path)
|
||||
}
|
||||
|
||||
for i, p := range expected {
|
||||
expected[i] = filepath.FromSlash(p)
|
||||
}
|
||||
|
||||
dirTracker := make(emptyDirTracker)
|
||||
for _, p := range paths {
|
||||
if p.isFile {
|
||||
dirTracker.addFile(p.path)
|
||||
} else {
|
||||
dirTracker.addDir(p.path)
|
||||
}
|
||||
}
|
||||
|
||||
result := dirTracker.emptyDirs()
|
||||
if diff, equal := messagediff.PrettyDiff(expected, result); !equal {
|
||||
t.Errorf("Incorrect empty directories list; got %v, expected %v\n%v", result, expected, diff)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user