ignore_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build integration
  16. package integration
  17. import (
  18. "log"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. "github.com/syncthing/syncthing/internal/symlinks"
  23. )
  24. func TestIgnores(t *testing.T) {
  25. // Clean and start a syncthing instance
  26. log.Println("Cleaning...")
  27. err := removeAll("s1", "h1/index")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. p := syncthingProcess{ // id1
  32. instance: "1",
  33. argv: []string{"-home", "h1"},
  34. port: 8081,
  35. apiKey: apiKey,
  36. }
  37. err = p.start()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. defer p.stop()
  42. // Create eight empty files and directories
  43. files := []string{"f1", "f2", "f3", "f4", "f11", "f12", "f13", "f14"}
  44. dirs := []string{"d1", "d2", "d3", "d4", "d11", "d12", "d13", "d14"}
  45. all := append(files, dirs...)
  46. for _, file := range files {
  47. fd, err := os.Create(filepath.Join("s1", file))
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. fd.Close()
  52. }
  53. for _, dir := range dirs {
  54. err := os.Mkdir(filepath.Join("s1", dir), 0755)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. }
  59. var syms []string
  60. if symlinksSupported() {
  61. syms = []string{"s1", "s2", "s3", "s4", "s11", "s12", "s13", "s14"}
  62. for _, sym := range syms {
  63. p := filepath.Join("s1", sym)
  64. symlinks.Create(p, p, 0)
  65. }
  66. all = append(all, syms...)
  67. }
  68. // Rescan and verify that we see them all
  69. p.post("/rest/scan?folder=default", nil)
  70. m, err := p.model("default")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. expected := len(all) // nothing is ignored
  75. if m.LocalFiles != expected {
  76. t.Fatalf("Incorrect number of files after initial scan, %d != %d", m.LocalFiles, expected)
  77. }
  78. // Add some of them to an ignore file
  79. fd, err := os.Create("s1/.stignore")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. _, err = fd.WriteString("f1*\nf2\nd1*\nd2\ns1*\ns2") // [fds][34] only non-ignored items
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. err = fd.Close()
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. // Rescan and verify that we see them
  92. p.post("/rest/scan?folder=default", nil)
  93. m, err = p.model("default")
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. expected = len(all) * 2 / 8 // two out of eight items of each type should remain
  98. if m.LocalFiles != expected {
  99. t.Fatalf("Incorrect number of files after first ignore, %d != %d", m.LocalFiles, expected)
  100. }
  101. // Change the pattern to include some of the files and dirs previously ignored
  102. fd, err = os.Create("s1/.stignore")
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. _, err = fd.WriteString("f2\nd2\ns2\n")
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. err = fd.Close()
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. // Rescan and verify that we see them
  115. p.post("/rest/scan?folder=default", nil)
  116. m, err = p.model("default")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. expected = len(all) * 7 / 8 // seven out of eight items of each type should remain
  121. if m.LocalFiles != expected {
  122. t.Fatalf("Incorrect number of files after second ignore, %d != %d", m.LocalFiles, expected)
  123. }
  124. }