external_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 http://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "testing"
  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. command: "nonexistant command",
  25. folderPath: "testdata/folder path",
  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"
  37. if runtime.GOOS == "windows" {
  38. cmd = `.\_external_test\external.bat`
  39. }
  40. file := "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. command: cmd,
  50. folderPath: "testdata/folder path",
  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 := ioutil.WriteFile(file, []byte("hello\n"), 0644); err != nil {
  68. t.Fatal(err)
  69. }
  70. }