ignore_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (C) 2014 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. // +build integration
  7. package integration
  8. import (
  9. "log"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "time"
  14. "github.com/syncthing/syncthing/internal/symlinks"
  15. )
  16. func TestIgnores(t *testing.T) {
  17. // Clean and start a syncthing instance
  18. log.Println("Cleaning...")
  19. err := removeAll("s1", "h1/index*")
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. p := syncthingProcess{ // id1
  24. instance: "1",
  25. argv: []string{"-home", "h1"},
  26. port: 8081,
  27. apiKey: apiKey,
  28. }
  29. err = p.start()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. // Wait for one scan to succeed, or up to 20 seconds... This is to let
  34. // startup, UPnP etc complete and make sure that we've performed folder
  35. // error checking which creates the folder path if it's missing.
  36. for i := 0; i < 20; i++ {
  37. err := p.rescan("default")
  38. if err != nil {
  39. time.Sleep(time.Second)
  40. continue
  41. }
  42. break
  43. }
  44. defer p.stop()
  45. // Create eight empty files and directories
  46. files := []string{"f1", "f2", "f3", "f4", "f11", "f12", "f13", "f14"}
  47. dirs := []string{"d1", "d2", "d3", "d4", "d11", "d12", "d13", "d14"}
  48. all := append(files, dirs...)
  49. for _, file := range files {
  50. fd, err := os.Create(filepath.Join("s1", file))
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. fd.Close()
  55. }
  56. for _, dir := range dirs {
  57. err := os.Mkdir(filepath.Join("s1", dir), 0755)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. var syms []string
  63. if symlinksSupported() {
  64. syms = []string{"s1", "s2", "s3", "s4", "s11", "s12", "s13", "s14"}
  65. for _, sym := range syms {
  66. p := filepath.Join("s1", sym)
  67. symlinks.Create(p, p, 0)
  68. }
  69. all = append(all, syms...)
  70. }
  71. // Rescan and verify that we see them all
  72. // Wait for one scan to succeed, or up to 20 seconds...
  73. // This is to let startup, UPnP etc complete.
  74. for i := 0; i < 20; i++ {
  75. err := p.rescan("default")
  76. if err != nil {
  77. time.Sleep(time.Second)
  78. continue
  79. }
  80. break
  81. }
  82. m, err := p.model("default")
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. expected := len(all) // nothing is ignored
  87. if m.LocalFiles != expected {
  88. t.Fatalf("Incorrect number of files after initial scan, %d != %d", m.LocalFiles, expected)
  89. }
  90. // Add some of them to an ignore file
  91. fd, err := os.Create("s1/.stignore")
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. _, err = fd.WriteString("f1*\nf2\nd1*\nd2\ns1*\ns2") // [fds][34] only non-ignored items
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. err = fd.Close()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. // Rescan and verify that we see them
  104. p.rescan("default")
  105. m, err = p.model("default")
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. expected = len(all) * 2 / 8 // two out of eight items of each type should remain
  110. if m.LocalFiles != expected {
  111. t.Fatalf("Incorrect number of files after first ignore, %d != %d", m.LocalFiles, expected)
  112. }
  113. // Change the pattern to include some of the files and dirs previously ignored
  114. fd, err = os.Create("s1/.stignore")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. _, err = fd.WriteString("f2\nd2\ns2\n")
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. err = fd.Close()
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. // Rescan and verify that we see them
  127. p.rescan("default")
  128. m, err = p.model("default")
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. expected = len(all) * 7 / 8 // seven out of eight items of each type should remain
  133. if m.LocalFiles != expected {
  134. t.Fatalf("Incorrect number of files after second ignore, %d != %d", m.LocalFiles, expected)
  135. }
  136. }