external_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "os"
  9. "path/filepath"
  10. "testing"
  11. "github.com/syncthing/syncthing/lib/build"
  12. "github.com/syncthing/syncthing/lib/fs"
  13. )
  14. func TestExternalNoCommand(t *testing.T) {
  15. file := "testdata/folder path/long filename.txt"
  16. prepForRemoval(t, file)
  17. defer os.RemoveAll("testdata")
  18. // The file should exist before the versioner run.
  19. if _, err := os.Lstat(file); err != nil {
  20. t.Fatal("File should exist")
  21. }
  22. // The versioner should fail due to missing command.
  23. e := external{
  24. filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
  25. command: "nonexistent command",
  26. }
  27. if err := e.Archive(file); err == nil {
  28. t.Error("Command should have failed")
  29. }
  30. // The file should not have been removed.
  31. if _, err := os.Lstat(file); err != nil {
  32. t.Fatal("File should still exist")
  33. }
  34. }
  35. func TestExternal(t *testing.T) {
  36. cmd := "./_external_test/external.sh %FOLDER_PATH% %FILE_PATH%"
  37. if build.IsWindows {
  38. cmd = `.\\_external_test\\external.bat %FOLDER_PATH% %FILE_PATH%`
  39. }
  40. file := filepath.Join("testdata", "folder path", "dir (parens)", "/long filename (parens).txt")
  41. prepForRemoval(t, file)
  42. defer os.RemoveAll("testdata")
  43. // The file should exist before the versioner run.
  44. if _, err := os.Lstat(file); err != nil {
  45. t.Fatal("File should exist")
  46. }
  47. // The versioner should run successfully.
  48. e := external{
  49. filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "."),
  50. command: cmd,
  51. }
  52. if err := e.Archive(file); err != nil {
  53. t.Fatal(err)
  54. }
  55. // The file should no longer exist.
  56. if _, err := os.Lstat(file); !os.IsNotExist(err) {
  57. t.Error("File should no longer exist")
  58. }
  59. }
  60. func prepForRemoval(t *testing.T, file string) {
  61. if err := os.RemoveAll("testdata"); err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
  65. t.Fatal(err)
  66. }
  67. if err := os.WriteFile(file, []byte("hello\n"), 0644); err != nil {
  68. t.Fatal(err)
  69. }
  70. }