2017-11-18 15:56:53 +00:00
|
|
|
// 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:
|
2022-08-03 13:43:26 +00:00
|
|
|
//
|
|
|
|
// .stversions/
|
|
|
|
// ├── keep1
|
|
|
|
// │ └── file1
|
|
|
|
// ├── keep2
|
|
|
|
// │ └── keep21
|
|
|
|
// │ └── keep22
|
|
|
|
// │ └── file1
|
|
|
|
// ├── remove1
|
|
|
|
// └── remove2
|
|
|
|
// └── remove21
|
|
|
|
// └── remove22
|
2017-11-18 15:56:53 +00:00
|
|
|
func TestEmptyDirs(t *testing.T) {
|
2024-11-19 10:32:56 +00:00
|
|
|
paths := []struct {
|
2017-11-18 15:56:53 +00:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
2024-11-19 10:32:56 +00:00
|
|
|
expected := []string{
|
2017-11-18 15:56:53 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
}
|