external_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "testing"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. )
  15. func TestExternalNoCommand(t *testing.T) {
  16. file := "testdata/folder path/long filename.txt"
  17. prepForRemoval(t, file)
  18. defer os.RemoveAll("testdata")
  19. // The file should exist before the versioner run.
  20. if _, err := os.Lstat(file); err != nil {
  21. t.Fatal("File should exist")
  22. }
  23. // The versioner should fail due to missing command.
  24. e := External{
  25. filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
  26. command: "nonexistent command",
  27. }
  28. if err := e.Archive(file); err == nil {
  29. t.Error("Command should have failed")
  30. }
  31. // The file should not have been removed.
  32. if _, err := os.Lstat(file); err != nil {
  33. t.Fatal("File should still exist")
  34. }
  35. }
  36. func TestExternal(t *testing.T) {
  37. cmd := "./_external_test/external.sh %FOLDER_PATH% %FILE_PATH%"
  38. if runtime.GOOS == "windows" {
  39. cmd = `.\\_external_test\\external.bat %FOLDER_PATH% %FILE_PATH%`
  40. }
  41. file := filepath.Join("testdata", "folder path", "dir (parens)", "/long filename (parens).txt")
  42. prepForRemoval(t, file)
  43. defer os.RemoveAll("testdata")
  44. // The file should exist before the versioner run.
  45. if _, err := os.Lstat(file); err != nil {
  46. t.Fatal("File should exist")
  47. }
  48. // The versioner should run successfully.
  49. e := External{
  50. filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
  51. command: cmd,
  52. }
  53. if err := e.Archive(file); err != nil {
  54. t.Fatal(err)
  55. }
  56. // The file should no longer exist.
  57. if _, err := os.Lstat(file); !os.IsNotExist(err) {
  58. t.Error("File should no longer exist")
  59. }
  60. }
  61. func prepForRemoval(t *testing.T, file string) {
  62. if err := os.RemoveAll("testdata"); err != nil {
  63. t.Fatal(err)
  64. }
  65. if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
  66. t.Fatal(err)
  67. }
  68. if err := ioutil.WriteFile(file, []byte("hello\n"), 0644); err != nil {
  69. t.Fatal(err)
  70. }
  71. }