From d146acd2e6eff6cc4e0d22493f2d5c0f3cbbbc80 Mon Sep 17 00:00:00 2001 From: Oleksandr Levchenkov Date: Thu, 4 Apr 2024 17:05:17 +0300 Subject: [PATCH] fix e2e init test for windows (#1461) Signed-off-by: ollevche --- internal/command/e2etest/init_test.go | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/command/e2etest/init_test.go b/internal/command/e2etest/init_test.go index fa71c26361..fc2c7255c5 100644 --- a/internal/command/e2etest/init_test.go +++ b/internal/command/e2etest/init_test.go @@ -7,6 +7,7 @@ package e2etest import ( "bytes" + "encoding/json" "fmt" "os" "path/filepath" @@ -465,8 +466,10 @@ func TestInitProviderNotFound(t *testing.T) { t.Fatal("expected error, got success") } - if !strings.Contains(stdout, `"diagnostic":{"severity":"error","summary":"Failed to query available provider packages","detail":"Could not retrieve the list of available versions for provider hashicorp/nonexist: provider registry.opentofu.org/hashicorp/nonexist was not found in any of the search locations\n\n - `+pluginDir+`"},"type":"diagnostic"}`) { - t.Errorf("expected error message is missing from output:\n%s", stdout) + escapedPluginDir := escapeStringJSON(pluginDir) + + if !strings.Contains(stdout, `"diagnostic":{"severity":"error","summary":"Failed to query available provider packages","detail":"Could not retrieve the list of available versions for provider hashicorp/nonexist: provider registry.opentofu.org/hashicorp/nonexist was not found in any of the search locations\n\n - `+escapedPluginDir+`"},"type":"diagnostic"}`) { + t.Errorf("expected error message is missing from output (pluginDir = '%s'):\n%s", escapedPluginDir, stdout) } }) @@ -522,3 +525,24 @@ func TestInitProviderNotFound(t *testing.T) { // } // //} + +func escapeStringJSON(v string) string { + b := &strings.Builder{} + + enc := json.NewEncoder(b) + + enc.SetEscapeHTML(false) + + if err := enc.Encode(v); err != nil { + panic("failed to escapeStringJSON: " + v) + } + + marshaledV := b.String() + + // shouldn't happen + if len(marshaledV) < 2 { + return string(marshaledV) + } + + return string(marshaledV[1 : len(marshaledV)-2]) +}