Repository: docker/compose
File: pkg/e2e/watch_test.go (Go)

Code before:
		})

	t.Logf("Sync and restart use case")
	require.NoError(t, os.Mkdir(configDir, 0o700))
	writeTestFile("file.config", "This is an updated config file", configDir)
	checkRestart := func(state string) poll.Check {
		return func(pollLog poll.LogT) poll.Result {
			if strings.Contains(r.Combined(), state) {
				return poll.Success()
			}
			return poll.Continue("%v", r.Combined())
		}
	}
	poll.WaitOn(t, checkRestart(fmt.Sprintf("service %q restarted", svcName)))
	poll.WaitOn(t, checkFileContents("/app/config/file.config", "This is an updated config file"))

	testComplete.Store(true)
}

func TestWatchExec(t *testing.T) {
	cli := NewCLI(t)
	const projectName = "test_watch_exec"

	t.Cleanup(func() {
		cli.RunDockerComposeCmd(t, "-p", projectName, "down")
	})

	tmpdir := t.TempDir()
	composeFilePath := filepath.Join(tmpdir, "compose.yaml")
	CopyFile(t, filepath.Join("fixtures", "watch", "exec.yaml"), composeFilePath)
	cmd := cli.NewDockerComposeCmd(t, "-p", projectName, "-f", composeFilePath, "up", "--watch")
	buffer := bytes.NewBuffer(nil)
	cmd.Stdout = buffer
	watch := icmd.StartCmd(cmd)

	poll.WaitOn(t, func(l poll.LogT) poll.Result {
		out := buffer.String()
		if strings.Contains(out, "64 bytes from") {
			return poll.Success()
		}
		return poll.Continue("%v", watch.Stdout())
	})

	t.Logf("Create new file")

	testFile := filepath.Join(tmpdir, "test")
	require.NoError(t, os.WriteFile(testFile, []byte("test\n"), 0o600))

	poll.WaitOn(t, func(l poll.LogT) poll.Result {
		out := buffer.String()
		if strings.Contains(out, "SUCCESS") {

Review comment (suggestion):
you can now use 😉 
```suggestion
	defer c.cleanupWithDown(t, projectName)
```

Code after:
		})

	t.Logf("Sync and restart use case")
	require.NoError(t, os.Mkdir(configDir, 0o700))
	writeTestFile("file.config", "This is an updated config file", configDir)
	checkRestart := func(state string) poll.Check {
		return func(pollLog poll.LogT) poll.Result {
			if strings.Contains(r.Combined(), state) {
				return poll.Success()
			}
			return poll.Continue("%v", r.Combined())
		}
	}
	poll.WaitOn(t, checkRestart(fmt.Sprintf("service %q restarted", svcName)))
	poll.WaitOn(t, checkFileContents("/app/config/file.config", "This is an updated config file"))

	testComplete.Store(true)
}

func TestWatchExec(t *testing.T) {
	c := NewCLI(t)
	const projectName = "test_watch_exec"

	defer c.cleanupWithDown(t, projectName)

	tmpdir := t.TempDir()
	composeFilePath := filepath.Join(tmpdir, "compose.yaml")
	CopyFile(t, filepath.Join("fixtures", "watch", "exec.yaml"), composeFilePath)
	cmd := c.NewDockerComposeCmd(t, "-p", projectName, "-f", composeFilePath, "up", "--watch")
	buffer := bytes.NewBuffer(nil)
	cmd.Stdout = buffer
	watch := icmd.StartCmd(cmd)

	poll.WaitOn(t, func(l poll.LogT) poll.Result {
		out := buffer.String()
		if strings.Contains(out, "64 bytes from") {
			return poll.Success()
		}
		return poll.Continue("%v", watch.Stdout())
	})

	t.Logf("Create new file")

	testFile := filepath.Join(tmpdir, "test")
	require.NoError(t, os.WriteFile(testFile, []byte("test\n"), 0o600))

	poll.WaitOn(t, func(l poll.LogT) poll.Result {
		out := buffer.String()
		if strings.Contains(out, "SUCCESS") {
			return poll.Success()
		}