diff --git a/internal/lsp/server_aggregates_test.go b/internal/lsp/server_aggregates_test.go index 6d0a4f8f..2523223c 100644 --- a/internal/lsp/server_aggregates_test.go +++ b/internal/lsp/server_aggregates_test.go @@ -57,8 +57,7 @@ import rego.v1 defer timeout.Stop() // no unresolved-imports at this stage - for { - var success bool + for success := false; !success; { select { case violations := <-messages["foo.rego"]: if slices.Contains(violations, "unresolved-import") { @@ -71,10 +70,6 @@ import rego.v1 case <-timeout.C: t.Fatalf("timed out waiting for expected foo.rego diagnostics") } - - if success { - break - } } barURI := fileURIScheme + filepath.Join(tempDir, "bar.rego") @@ -99,8 +94,7 @@ import rego.v1 // unresolved-imports is now expected timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case violations := <-messages["foo.rego"]: if !slices.Contains(violations, "unresolved-import") { @@ -113,10 +107,6 @@ import rego.v1 case <-timeout.C: t.Fatalf("timed out waiting for expected foo.rego diagnostics") } - - if success { - break - } } fooURI := fileURIScheme + filepath.Join(tempDir, "foo.rego") @@ -144,8 +134,7 @@ import data.qux # new name for bar.rego package // unresolved-imports is again not expected timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case violations := <-messages["foo.rego"]: if slices.Contains(violations, "unresolved-import") { @@ -158,10 +147,6 @@ import data.qux # new name for bar.rego package case <-timeout.C: t.Fatalf("timed out waiting for expected foo.rego diagnostics") } - - if success { - break - } } } @@ -206,9 +191,7 @@ import data.quz ticker := time.NewTicker(500 * time.Millisecond) defer ticker.Stop() - for { - success := false - + for success := false; !success; { select { case <-ticker.C: aggs := ls.cache.GetFileAggregates() @@ -222,10 +205,6 @@ import data.quz case <-timeout.C: t.Fatalf("timed out waiting for file aggregates to be set") } - - if success { - break - } } determineImports := func(aggs map[string][]report.Aggregate) []string { @@ -292,9 +271,7 @@ import data.wow # new timeout.Reset(determineTimeout()) - for { - success := false - + for success := false; !success; { select { case <-ticker.C: imports = determineImports(ls.cache.GetFileAggregates()) @@ -309,10 +286,6 @@ import data.wow # new case <-timeout.C: t.Fatalf("timed out waiting for file aggregates to be set") } - - if success { - break - } } } @@ -358,8 +331,7 @@ import rego.v1 timeout := time.NewTimer(determineTimeout()) defer timeout.Stop() - for { - var success bool + for success := true; !success; { select { case violations := <-messages["foo.rego"]: if !slices.Contains(violations, "unresolved-import") { @@ -378,10 +350,6 @@ import rego.v1 case <-timeout.C: t.Fatalf("timed out waiting for foo.rego diagnostics") } - - if success { - break - } } // update the contents of the bar.rego file to address the unresolved-import @@ -407,8 +375,7 @@ import rego.v1 // wait for foo.rego to have the correct violations timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case violations := <-messages["foo.rego"]: if slices.Contains(violations, "unresolved-import") { @@ -427,10 +394,6 @@ import rego.v1 case <-timeout.C: t.Fatalf("timed out waiting for foo.rego diagnostics") } - - if success { - break - } } // update the contents of the bar.rego to bring back the violation @@ -454,8 +417,7 @@ import rego.v1 // check the violation is back timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case violations := <-messages["foo.rego"]: if !slices.Contains(violations, "unresolved-import") { @@ -474,9 +436,5 @@ import rego.v1 case <-timeout.C: t.Fatalf("timed out waiting for foo.rego diagnostics") } - - if success { - break - } } } diff --git a/internal/lsp/server_config_test.go b/internal/lsp/server_config_test.go index 5d72bf64..ecb3276c 100644 --- a/internal/lsp/server_config_test.go +++ b/internal/lsp/server_config_test.go @@ -87,18 +87,13 @@ allow := true timeout := time.NewTimer(determineTimeout()) defer timeout.Stop() - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: success = testRequestDataCodes(t, requestData, mainRegoFileURI, []string{"opa-fmt"}) case <-timeout.C: t.Fatalf("timed out waiting for file diagnostics to be sent") } - - if success { - break - } } // User updates config file contents in parent directory that is not @@ -120,18 +115,13 @@ allow := true // validate that the client received a new, empty diagnostics notification for the file timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: success = testRequestDataCodes(t, requestData, mainRegoFileURI, []string{}) case <-timeout.C: t.Fatalf("timed out waiting for file diagnostics to be sent") } - - if success { - break - } } } @@ -164,8 +154,7 @@ func TestLanguageServerCachesEnabledRulesAndUsesDefaultConfig(t *testing.T) { timeout := time.NewTimer(3 * time.Second) ticker := time.NewTicker(500 * time.Millisecond) - for { - var success bool + for success := false; !success; { select { case <-ticker.C: enabledRules := ls.getEnabledNonAggregateRules() @@ -181,10 +170,6 @@ func TestLanguageServerCachesEnabledRulesAndUsesDefaultConfig(t *testing.T) { case <-timeout.C: t.Fatalf("timed out waiting for enabled rules to be correct") } - - if success { - break - } } err = os.MkdirAll(filepath.Join(tempDir, ".regal"), 0o755) @@ -221,8 +206,7 @@ rules: timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case <-ticker.C: enabledRules := ls.getEnabledNonAggregateRules() @@ -244,10 +228,6 @@ rules: case <-timeout.C: t.Fatalf("timed out waiting for enabled rules to be correct") } - - if success { - break - } } configContents2 := ` @@ -270,8 +250,7 @@ rules: timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case <-ticker.C: enabledRules := ls.getEnabledNonAggregateRules() @@ -299,9 +278,5 @@ rules: case <-timeout.C: t.Fatalf("timed out waiting for enabled rules to be correct") } - - if success { - break - } } } diff --git a/internal/lsp/server_multi_file_test.go b/internal/lsp/server_multi_file_test.go index 15117dc5..9a684459 100644 --- a/internal/lsp/server_multi_file_test.go +++ b/internal/lsp/server_multi_file_test.go @@ -74,8 +74,7 @@ ignore: // wait for the aggregate data to be set, required for correct lint in next // step - for { - var success bool + for success := false; !success; { select { default: uri := "file://" + filepath.Join(tempDir, "admins.rego") @@ -83,23 +82,20 @@ ignore: aggs := ls.cache.GetFileAggregates(uri) if len(aggs) > 0 { success = true + + break // don't sleep } time.Sleep(500 * time.Millisecond) case <-timeout.C: t.Fatalf("timed out waiting admin aggregates to be set") } - - if success { - break - } } timeout.Reset(determineTimeout()) // validate that the client received a diagnostics notification for authz.rego - for { - var success bool + for success := false; !success; { select { case violations := <-messages["authz.rego"]: if !slices.Contains(violations, "prefer-package-imports") { @@ -112,17 +108,12 @@ ignore: case <-timeout.C: t.Fatalf("timed out waiting for authz.rego diagnostics to be sent") } - - if success { - break - } } // validate that the client received a diagnostics notification for admins.rego timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case violations := <-messages["admins.rego"]: if !slices.Contains(violations, "use-assignment-operator") { @@ -135,10 +126,6 @@ ignore: case <-timeout.C: t.Fatalf("timed out waiting for admins.rego diagnostics to be sent") } - - if success { - break - } } // 3. Client sends textDocument/didChange notification with new contents @@ -171,8 +158,7 @@ allow if input.user in admins.users // authz.rego should now have no violations timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case violations := <-messages["authz.rego"]: if len(violations) > 0 { @@ -185,9 +171,5 @@ allow if input.user in admins.users case <-timeout.C: t.Fatalf("timed out waiting for authz.rego diagnostics to be sent") } - - if success { - break - } } } diff --git a/internal/lsp/server_single_file_test.go b/internal/lsp/server_single_file_test.go index 7b1306d8..c236a8ef 100644 --- a/internal/lsp/server_single_file_test.go +++ b/internal/lsp/server_single_file_test.go @@ -84,18 +84,13 @@ rules: timeout := time.NewTimer(determineTimeout()) defer timeout.Stop() - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: success = testRequestDataCodes(t, requestData, mainRegoURI, []string{"opa-fmt", "use-assignment-operator"}) case <-timeout.C: t.Fatalf("timed out waiting for file diagnostics to be sent") } - - if success { - break - } } // Client sends textDocument/didChange notification with new contents for main.rego @@ -119,18 +114,13 @@ allow := true // validate that the client received a new diagnostics notification for the file timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: success = testRequestDataCodes(t, requestData, mainRegoURI, []string{"opa-fmt"}) case <-timeout.C: t.Fatalf("timed out waiting for file diagnostics to be sent") } - - if success { - break - } } // config update is caught by the config watcher @@ -151,8 +141,7 @@ rules: // validate that the client received a new, empty diagnostics notification for the file timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: if requestData.URI != mainRegoURI { @@ -176,10 +165,6 @@ rules: case <-timeout.C: t.Fatalf("timed out waiting for main.rego diagnostics to be sent") } - - if success { - break - } } // Client sends new config with an EOPA capabilities file specified. @@ -204,8 +189,7 @@ capabilities: // validate that the client received a new, empty diagnostics notification for the file timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: if requestData.URI != mainRegoURI { @@ -229,10 +213,6 @@ capabilities: case <-timeout.C: t.Fatalf("timed out waiting for main.rego diagnostics to be sent") } - - if success { - break - } } // Client sends textDocument/didChange notification with new @@ -262,8 +242,7 @@ allow := neo4j.q // validate that the client received a new diagnostics notification for the file timeout.Reset(determineTimeout()) - for { - var success bool + for success := false; !success; { select { case requestData := <-receivedMessages: if requestData.URI != mainRegoURI { @@ -287,10 +266,6 @@ allow := neo4j.q case <-timeout.C: t.Fatalf("timed out waiting for file diagnostics to be sent") } - - if success { - break - } } // 7. With our new config applied, and the file updated, we can ask the @@ -302,9 +277,7 @@ allow := neo4j.q ticker := time.NewTicker(500 * time.Millisecond) defer ticker.Stop() - for { - foundNeo4j := false - + for success := false; !success; { select { case <-ticker.C: // Create a new context with timeout for each request, this is @@ -346,7 +319,7 @@ allow := neo4j.q } if label == "neo4j.query" { - foundNeo4j = true + success = true break } @@ -356,9 +329,5 @@ allow := neo4j.q case <-timeout.C: t.Fatalf("timed out waiting for file completion to correct") } - - if foundNeo4j { - break - } } } diff --git a/internal/lsp/server_template_test.go b/internal/lsp/server_template_test.go index 41697c64..495a1372 100644 --- a/internal/lsp/server_template_test.go +++ b/internal/lsp/server_template_test.go @@ -319,8 +319,7 @@ func TestNewFileTemplating(t *testing.T) { "label": "Template new Rego file" }`, newFileURI, expectedNewFileURI, tempDir) - for { - var success bool + for success := false; !success; { select { case msg := <-receivedMessages: t.Log("received message:", string(msg)) @@ -344,17 +343,11 @@ func TestNewFileTemplating(t *testing.T) { } } - if allLinesMatch { - success = true - } + success = allLinesMatch case <-timeout.C: t.Log("never received expected message", expectedMessage) t.Fatalf("timed out waiting for expected message to be sent") } - - if success { - break - } } }