Repository: opentofu/opentofu
File: internal/command/e2etest/encryption_test.go (Go)

Code before:
	}
	if r.err != nil {
		debug.PrintStack()
		r.t.Fatalf("unexpected error: %s", r.err)
	}

	return r
}

func (r tofuResult) Failure() tofuResult {
	if r.err == nil {
		debug.PrintStack()
		r.t.Fatal("expected error")
	}
	return r
}

func (r tofuResult) StderrContains(msg string) tofuResult {
	if !strings.Contains(r.stderr, msg) {
		debug.PrintStack()
		r.t.Fatalf("expected stderr output %q:\n%s", msg, r.stderr)
	}
	return r
}

func SanitizeStderr(result *tofuResult) {
	// ANSI escape sequence regex for removing terminal color codes and control characters
	var ansiRegex = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
	result.stderr = ansiRegex.ReplaceAllString(result.stderr, "")
	//Pipe and carriage return replacement in order to correctly sanitze the stderr output
	result.stderr = strings.ReplaceAll(
		strings.ReplaceAll(result.stderr, "│", ""),
		"\n", "",
	)
}

func (r tofuResult) Contains(msg string) tofuResult {
	if !strings.Contains(r.stdout, msg) {
		debug.PrintStack()
		r.t.Fatalf("expected output %q:\n%s", msg, r.stdout)
	}
	return r
}

// This test covers the scenario where a user migrates an existing project
// to having encryption enabled, uses it, then migrates back to encryption
// disabled
func TestEncryptionFlow(t *testing.T) {

	// This test reaches out to registry.opentofu.org to download the
	// mock provider, so it can only run if network access is allowed

Review comment (refactor):
`improvement`
This is indeed one way to do it. Though, not really agree with changing the value of the `*tofuResult` in the process.

One more thing, would be better to move this function `strip_ansi.go`, and reuse the already existing function in there (`stripAnsi`).

Code after:
	if r.err != nil {
		debug.PrintStack()
		r.t.Fatalf("unexpected error: %s", r.err)
	}

	return r
}

func (r tofuResult) Failure() tofuResult {
	if r.err == nil {
		debug.PrintStack()
		r.t.Fatal("expected error")
	}
	return r
}

func SanitizeStderr(msg string) string {
	// ANSI escape sequence regex for removing terminal color codes and control characters
	msg = stripAnsi(msg)
	//Pipe and carriage return replacement in order to correctly sanitze the stderr output
	msg = strings.ReplaceAll(
		strings.ReplaceAll(msg, "│", ""),
		"\n", "",
	)
	return msg
}

func (r tofuResult) StderrContains(msg string) tofuResult {
	stdErrSanitized := SanitizeStderr(r.stderr)
	if !strings.Contains(stdErrSanitized, msg) {
		debug.PrintStack()
		r.t.Fatalf("expected stderr output %q:\n%s", msg, stdErrSanitized)
	}
	return r
}

func (r tofuResult) Contains(msg string) tofuResult {
	if !strings.Contains(r.stdout, msg) {
		debug.PrintStack()
		r.t.Fatalf("expected output %q:\n%s", msg, r.stdout)
	}
	return r
}

// This test covers the scenario where a user migrates an existing project
// to having encryption enabled, uses it, then migrates back to encryption
// disabled
func TestEncryptionFlow(t *testing.T) {

	// This test reaches out to registry.opentofu.org to download the
	// mock provider, so it can only run if network access is allowed