浏览代码

lib/versioner: Test for external versioner

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3609
Jakob Borg 9 年之前
父节点
当前提交
90c0a39df8
共有 3 个文件被更改,包括 99 次插入0 次删除
  1. 5 0
      lib/versioner/_external_test/external.bat
  2. 5 0
      lib/versioner/_external_test/external.sh
  3. 89 0
      lib/versioner/external_test.go

+ 5 - 0
lib/versioner/_external_test/external.bat

@@ -0,0 +1,5 @@
+set FOLDER_PATH=%~1
+set FILE_PATH=%~2
+echo "1--%FOLDER_PATH%--"
+echo "2--%FILE_PATH%--"
+del "%FOLDER_PATH%\%FILE_PATH%"

+ 5 - 0
lib/versioner/_external_test/external.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+
+echo "1--$1--"
+echo "2--$2--"
+rm -f "$1/$2"

+ 89 - 0
lib/versioner/external_test.go

@@ -0,0 +1,89 @@
+// 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 http://mozilla.org/MPL/2.0/.
+
+package versioner
+
+import (
+	"io/ioutil"
+	"os"
+	"path/filepath"
+	"runtime"
+	"testing"
+)
+
+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{
+		command:    "nonexistant command",
+		folderPath: "testdata/folder path",
+	}
+	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"
+	if runtime.GOOS == "windows" {
+		cmd = `.\_external_test\external.bat`
+	}
+
+	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 run successfully.
+
+	e := External{
+		command:    cmd,
+		folderPath: "testdata/folder path",
+	}
+	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)
+	}
+}