Repository: syncthing/syncthing
File: lib/model/folder_recvonly_test.go (Go)

Code before:
	if size.Files != 1 || size.Directories != 1 {
		t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
	}
	size = needSizeLocal(t, m, "ro")
	if size.Files+size.Directories > 0 {
		t.Fatalf("Need: expected nothing: %+v", size)
	}
	size = receiveOnlyChangedSize(t, m, "ro")
	if size.Files+size.Directories > 0 {
		t.Fatalf("ROChanged: expected nothing: %+v", size)
	}

	// Modify the file

	writeFilePerm(t, ffs, "knownDir/knownFile", []byte("change1\n"), 0o644)

	must(t, m.ScanFolder("ro"))

	size = receiveOnlyChangedSize(t, m, "ro")
	if size.Files != 1 {
		t.Fatalf("Receive only: expected 1 file: %+v", size)
	}

	  // Perform another modification. This should not cause the file to be needed.
	  // This is a regression test: Previously on scan the file version was changed to conflict with the global
	  // version, thus being needed and creating a conflict copy on next pull.

	writeFilePerm(t, ffs, "knownDir/knownFile", []byte("change2\n"), 0o644)

	must(t, m.ScanFolder("ro"))

	size = needSizeLocal(t, m, "ro")
	if size.Files != 0 {
		t.Fatalf("Need: expected nothing: %+v", size)
	}
}

func setupKnownFiles(t *testing.T, ffs fs.Filesystem, data []byte) []protocol.FileInfo {
	t.Helper()

	must(t, ffs.MkdirAll("knownDir", 0o755))
	writeFilePerm(t, ffs, "knownDir/knownFile", data, 0o644)

	t0 := time.Now().Add(-1 * time.Minute)
	must(t, ffs.Chtimes("knownDir/knownFile", t0, t0))

	fi, err := ffs.Stat("knownDir/knownFile")
	if err != nil {
		t.Fatal(err)
	}
	blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), protocol.BlockSize(int64(len(data))), int64(len(data)), nil, true)

Review comment (suggestion):
```suggestion
	// Perform another modification. This should not cause the file to be needed.
	// This is a regression test: Previously on scan the file version was changed to conflict with the global
	// version, thus being needed and creating a conflict copy on next pull.
```

Code after:
	if size.Files != 1 || size.Directories != 1 {
		t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
	}
	size = needSizeLocal(t, m, "ro")
	if size.Files+size.Directories > 0 {
		t.Fatalf("Need: expected nothing: %+v", size)
	}
	size = receiveOnlyChangedSize(t, m, "ro")
	if size.Files+size.Directories > 0 {
		t.Fatalf("ROChanged: expected nothing: %+v", size)
	}

	// Modify the file

	writeFilePerm(t, ffs, "knownDir/knownFile", []byte("change1\n"), 0o644)

	must(t, m.ScanFolder("ro"))

	size = receiveOnlyChangedSize(t, m, "ro")
	if size.Files != 1 {
		t.Fatalf("Receive only: expected 1 file: %+v", size)
	}

	// Perform another modification. This should not cause the file to be needed.
	// This is a regression test: Previously on scan the file version was changed to conflict with the global
	// version, thus being needed and creating a conflict copy on next pull.

	writeFilePerm(t, ffs, "knownDir/knownFile", []byte("change2\n"), 0o644)

	must(t, m.ScanFolder("ro"))

	size = needSizeLocal(t, m, "ro")
	if size.Files != 0 {
		t.Fatalf("Need: expected nothing: %+v", size)
	}
}

func setupKnownFiles(t *testing.T, ffs fs.Filesystem, data []byte) []protocol.FileInfo {
	t.Helper()

	must(t, ffs.MkdirAll("knownDir", 0o755))
	writeFilePerm(t, ffs, "knownDir/knownFile", data, 0o644)

	t0 := time.Now().Add(-1 * time.Minute)
	must(t, ffs.Chtimes("knownDir/knownFile", t0, t0))

	fi, err := ffs.Stat("knownDir/knownFile")
	if err != nil {
		t.Fatal(err)
	}
	blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), protocol.BlockSize(int64(len(data))), int64(len(data)), nil, true)