Skip to content

Commit

Permalink
chore: Use t.Setenv in tests (open-policy-agent#5321)
Browse files Browse the repository at this point in the history
And enable the `tenv` linter for the future.

Also, bump version of golangci-lint and fix some new
warnings that came from that.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Oct 27, 2022
1 parent 6ec07ca commit 50d4e31
Show file tree
Hide file tree
Showing 24 changed files with 95 additions and 143 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
- varcheck
- deadcode
- misspell
- tenv
- typecheck
- structcheck
- staticcheck
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ifeq ($(WASM_ENABLED),1)
GO_TAGS = -tags=opa_wasm
endif

GOLANGCI_LINT_VERSION := v1.46.2
GOLANGCI_LINT_VERSION := v1.50.1

DOCKER_RUNNING ?= $(shell docker ps >/dev/null 2>&1 && echo 1 || echo 0)

Expand Down
46 changes: 23 additions & 23 deletions ast/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
//
// Rego policies are typically defined in text files and then parsed and compiled by the policy engine at runtime. The parsing stage takes the text or string representation of the policy and converts it into an abstract syntax tree (AST) that consists of the types mentioned above. The AST is organized as follows:
//
// Module
// |
// +--- Package (Reference)
// |
// +--- Imports
// | |
// | +--- Import (Term)
// |
// +--- Rules
// |
// +--- Rule
// |
// +--- Head
// | |
// | +--- Name (Variable)
// | |
// | +--- Key (Term)
// | |
// | +--- Value (Term)
// |
// +--- Body
// |
// +--- Expression (Term | Terms | Variable Declaration)
// Module
// |
// +--- Package (Reference)
// |
// +--- Imports
// | |
// | +--- Import (Term)
// |
// +--- Rules
// |
// +--- Rule
// |
// +--- Head
// | |
// | +--- Name (Variable)
// | |
// | +--- Key (Term)
// | |
// | +--- Value (Term)
// |
// +--- Body
// |
// +--- Expression (Term | Terms | Variable Declaration)
//
// At query time, the policy engine expects policies to have been compiled. The compilation stage takes one or more modules and compiles them into a format that the policy engine supports.
package ast
7 changes: 5 additions & 2 deletions ast/term_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ var bs []byte

// BenchmarkObjectString generates several objects of different sizes, and
// marshals them to JSON via two ways:
// map[string]int -> ast.Value -> .String()
//
// map[string]int -> ast.Value -> .String()
//
// and
// map[string]int -> json.Marshal()
//
// map[string]int -> json.Marshal()
//
// The difference between these two is relevant for feeding input into the
// wasm vm: when calling rego.New(...) with rego.Target("wasm"), it's up to
Expand Down
1 change: 1 addition & 0 deletions capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ import (
// FS contains the embedded capabilities/ directory of the built version,
// which has all the capabilities of previous versions:
// "v0.18.0.json" contains the capabilities JSON of version v0.18.0, etc
//
//go:embed *.json
var FS embed.FS
5 changes: 2 additions & 3 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) {
baseURL, teardown := getTestServer(exp, http.StatusOK)
defer teardown()

os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)
t.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)

var stdout bytes.Buffer

Expand All @@ -66,7 +65,7 @@ func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) {

func TestCheckOPAUpdateBadURL(t *testing.T) {
url := "http://foo:8112"
os.Setenv("OPA_TELEMETRY_SERVICE_URL", url)
t.Setenv("OPA_TELEMETRY_SERVICE_URL", url)

err := checkOPAUpdate(nil)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions dependencies/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ func filter(rs []ast.Ref, pred func(ast.Ref, ast.Ref) bool) (filtered []ast.Ref)
// complicated. It should be possible to compute all dependencies in two
// passes:
//
// 1) perform syntactic unification on vars
// 2) gather all refs rooted at data after plugging the head with substitution
// from (1)
// 1. perform syntactic unification on vars
// 2. gather all refs rooted at data after plugging the head with substitution
// from (1)
func ruleDeps(rule *ast.Rule) (resolved []ast.Ref) {
vars, others := extractEq(rule.Body)
joined := joinVarRefs(vars)
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func setTestEnvVar(t *testing.T, name, value string) string {
envKey := fmt.Sprintf("%s_%s", t.Name(), name)
os.Setenv(envKey, value)
t.Setenv(envKey, value)
return envKey
}

Expand Down
1 change: 0 additions & 1 deletion internal/gojsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (d *Schema) SetRootSchemaName(name string) {
// Pretty long function ( sorry :) )... but pretty straight forward, repetitive and boring
// Not much magic involved here, most of the job is to validate the key names and their values,
// then the values are copied into SubSchema struct
//
func (d *Schema) parseSchema(documentNode interface{}, currentSchema *SubSchema) error {

if currentSchema.Draft == nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/gojsonschema/schemaLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (sl *SchemaLoader) AddSchemas(loaders ...JSONLoader) error {
return nil
}

//AddSchema adds a schema under the provided URL to the schema cache
// AddSchema adds a schema under the provided URL to the schema cache
func (sl *SchemaLoader) AddSchema(url string, loader JSONLoader) error {

ref, err := gojsonreference.NewJsonReference(url)
Expand Down
5 changes: 2 additions & 3 deletions internal/jwx/jws/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// If you do not care about the details, the only things that you
// would need to use are the following functions:
//
// jws.SignWithOption(Payload, algorithm, key)
// jws.Verify(encodedjws, algorithm, key)
// jws.SignWithOption(Payload, algorithm, key)
// jws.Verify(encodedjws, algorithm, key)
//
// To sign, simply use `jws.SignWithOption`. `Payload` is a []byte buffer that
// contains whatever data you want to sign. `alg` is one of the
Expand Down Expand Up @@ -38,7 +38,6 @@ import (
// SignLiteral generates a Signature for the given Payload and Headers, and serializes
// it in compact serialization format. In this format you may NOT use
// multiple signers.
//
func SignLiteral(payload []byte, alg jwa.SignatureAlgorithm, key interface{}, hdrBuf []byte, rnd io.Reader) ([]byte, error) {
encodedHdr := base64.RawURLEncoding.EncodeToString(hdrBuf)
encodedPayload := base64.RawURLEncoding.EncodeToString(payload)
Expand Down
2 changes: 1 addition & 1 deletion internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
//
// Override at build time via:
//
// -ldflags "-X github.com/open-policy-agent/opa/internal/report.ExternalServiceURL=<url>"
// -ldflags "-X github.com/open-policy-agent/opa/internal/report.ExternalServiceURL=<url>"
//
// This will be overridden if the OPA_TELEMETRY_SERVICE_URL environment variable
// is provided.
Expand Down
10 changes: 4 additions & 6 deletions internal/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
)

func TestNewReportDefaultURL(t *testing.T) {
os.Unsetenv("OPA_TELEMETRY_SERVICE_URL")

reporter, err := New("", Options{})
if err != nil {
Expand All @@ -34,7 +32,7 @@ func TestSendReportBadRespStatus(t *testing.T) {
baseURL, teardown := getTestServer(nil, http.StatusBadRequest)
defer teardown()

os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)
t.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)

reporter, err := New("", Options{})
if err != nil {
Expand All @@ -59,7 +57,7 @@ func TestSendReportDecodeError(t *testing.T) {
baseURL, teardown := getTestServer("foo", http.StatusOK)
defer teardown()

os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)
t.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)

reporter, err := New("", Options{})
if err != nil {
Expand All @@ -85,7 +83,7 @@ func TestSendReportWithOPAUpdate(t *testing.T) {
baseURL, teardown := getTestServer(exp, http.StatusOK)
defer teardown()

os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)
t.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)

reporter, err := New("", Options{})
if err != nil {
Expand All @@ -108,7 +106,7 @@ func TestReportWithHeapStats(t *testing.T) {
baseURL, teardown := getTestServer(nil, http.StatusOK)
defer teardown()

os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)
t.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)

reporter, err := New("", Options{})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/strvals/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

/*Package strvals provides tools for working with strval lines.
/*
Package strvals provides tools for working with strval lines.
OPA runtime config supports a compressed format for YAML settings which we call strvals.
The format is roughly like this:
Expand Down
10 changes: 5 additions & 5 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import (
// configuration blob. If your plugin has not been configured, your
// factory will not be invoked.
//
// plugins:
// my_plugin1:
// some_key: foo
// # my_plugin2:
// # some_key2: bar
// plugins:
// my_plugin1:
// some_key: foo
// # my_plugin2:
// # some_key2: bar
//
// If OPA was started with the configuration above and received two
// calls to runtime.RegisterPlugins (one with NAME "my_plugin1" and
Expand Down
Loading

0 comments on commit 50d4e31

Please sign in to comment.