ignore_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 https://mozilla.org/MPL/2.0/.
  6. //go:build integration
  7. // +build integration
  8. package integration
  9. import (
  10. "log"
  11. "os"
  12. "path/filepath"
  13. "testing"
  14. "time"
  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 := startInstance(t, 1)
  24. defer checkedStop(t, p)
  25. // Create eight empty files and directories
  26. dirs := []string{"d1", "d2", "d3", "d4", "d11", "d12", "d13", "d14"}
  27. files := []string{"f1", "f2", "f3", "f4", "f11", "f12", "f13", "f14", "d1/f1.TXT"}
  28. for _, dir := range dirs {
  29. err := os.Mkdir(filepath.Join("s1", dir), 0o755)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. }
  34. for _, file := range files {
  35. fd, err := os.Create(filepath.Join("s1", file))
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. fd.Close()
  40. }
  41. // Rescan and verify that we see them all
  42. if err := p.Rescan("default"); err != nil {
  43. t.Fatal(err)
  44. }
  45. m, err := p.Model("default")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. expected := len(files) // nothing is ignored
  50. if m.LocalFiles != expected {
  51. t.Fatalf("Incorrect number of files after initial scan, %d != %d", m.LocalFiles, expected)
  52. }
  53. // Add some of them to an ignore file
  54. err = os.WriteFile("s1/.stignore",
  55. []byte("f1*\nf2\nd1*\nd2\ns1*\ns2\n(?i)*.txt"), // [fds][34] only non-ignored items
  56. 0o644)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. // Rescan and verify that we see them
  61. if err := p.Rescan("default"); err != nil {
  62. t.Fatal(err)
  63. }
  64. m, err = p.Model("default")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. expected = len(files) * 2 / 8 // two out of eight items should remain
  69. if m.LocalFiles != expected {
  70. t.Fatalf("Incorrect number of files after first ignore, %d != %d", m.LocalFiles, expected)
  71. }
  72. // Change the pattern to include some of the files and dirs previously ignored
  73. time.Sleep(1100 * time.Millisecond)
  74. err = os.WriteFile("s1/.stignore", []byte("f2\nd2\ns2\n"), 0o644)
  75. // Rescan and verify that we see them
  76. if err := p.Rescan("default"); err != nil {
  77. t.Fatal(err)
  78. }
  79. m, err = p.Model("default")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. expected = len(files)*7/8 + 1 // seven out of eight items should remain, plus the foo.TXT
  84. if m.LocalFiles != expected {
  85. t.Fatalf("Incorrect number of files after second ignore, %d != %d", m.LocalFiles, expected)
  86. }
  87. }