syncthing/lib/versioner/external_test.go
Jakob Borg 4e151d380c lib/versioner: Reduce surface area (#6186)
* lib/versioner: Reduce surface area

This is a refactor while I was anyway rooting around in the versioner.
Instead of exporting every possible implementation and the factory and
letting the caller do whatever, this now encapsulates all that and
exposes a New() that takes a config.VersioningConfiguration.

Given that and that we don't know (from the outside) how a versioner
works or what state it keeps, we now just construct it once per folder
and keep it around. Previously it was recreated for each restore
request.

* unparam

* wip
2019-11-26 07:39:31 +00:00

92 lines
2.1 KiB
Go

// Copyright (C) 2016 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 (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/syncthing/syncthing/lib/fs"
)
func TestExternalNoCommand(t *testing.T) {
file := "testdata/folder path/long filename.txt"
prepForRemoval(t, file)
defer os.RemoveAll("testdata")
// The file should exist before the versioner run.
if _, err := os.Lstat(file); err != nil {
t.Fatal("File should exist")
}
// The versioner should fail due to missing command.
e := external{
filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
command: "nonexistent command",
}
if err := e.Archive(file); err == nil {
t.Error("Command should have failed")
}
// The file should not have been removed.
if _, err := os.Lstat(file); err != nil {
t.Fatal("File should still exist")
}
}
func TestExternal(t *testing.T) {
cmd := "./_external_test/external.sh %FOLDER_PATH% %FILE_PATH%"
if runtime.GOOS == "windows" {
cmd = `.\\_external_test\\external.bat %FOLDER_PATH% %FILE_PATH%`
}
file := filepath.Join("testdata", "folder path", "dir (parens)", "/long filename (parens).txt")
prepForRemoval(t, file)
defer os.RemoveAll("testdata")
// The file should exist before the versioner run.
if _, err := os.Lstat(file); err != nil {
t.Fatal("File should exist")
}
// The versioner should run successfully.
e := external{
filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
command: cmd,
}
if err := e.Archive(file); err != nil {
t.Fatal(err)
}
// The file should no longer exist.
if _, err := os.Lstat(file); !os.IsNotExist(err) {
t.Error("File should no longer exist")
}
}
func prepForRemoval(t *testing.T, file string) {
if err := os.RemoveAll("testdata"); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(file, []byte("hello\n"), 0644); err != nil {
t.Fatal(err)
}
}