Skip to content

Commit

Permalink
test(gazelle): Use json instead of color logs
Browse files Browse the repository at this point in the history
Rely on json formatted logs to avoid postprecessing and unstable color format.
  • Loading branch information
stevebarrau authored Sep 4, 2023
1 parent 8ff3022 commit fa1fe87
Show file tree
Hide file tree
Showing 66 changed files with 57 additions and 83 deletions.
23 changes: 17 additions & 6 deletions java/gazelle/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ type javaLang struct {
func NewLanguage() language.Language {
goLevel, javaLevel := logconfig.LogLevel()

logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).
With().
Timestamp().
Caller().
Logger().
Level(goLevel)
var logger zerolog.Logger
if os.Getenv("GAZELLE_JAVA_LOG_FORMAT") == "json" {
logger = zerolog.New(os.Stderr)
} else {
logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr})
}

if os.Getenv("GAZELLE_JAVA_LOG_TIMESTAMP") != "false" {
logger = logger.With().Timestamp().Logger()
}

if os.Getenv("GAZELLE_JAVA_LOG_CALLER") != "false" {
logger = logger.With().Caller().Logger()
}

logger = logger.Level(goLevel)

logger.Debug().Msg("creating java language")

l := javaLang{
Expand Down
15 changes: 0 additions & 15 deletions java/gazelle/private/testutil/reset_zerolog_timestamps/BUILD.bazel

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 5 additions & 4 deletions java/gazelle/testdata/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ load("@bazel_gazelle//:def.bzl", "gazelle_binary", "gazelle_generation_test")
gazelle_binary(
name = "gazelle",
languages = [
# Register a fake language which makes timestamps consistent via static initializers.
# Otherwise we end up with stderr diffs every time we run the tests beacuse of timestamps.
# (Yes, this is a horrible hack).
"//java/gazelle/private/testutil/reset_zerolog_timestamps",
"@bazel_gazelle//language/proto:go_default_library",
"//java/gazelle",
],
Expand All @@ -32,6 +28,11 @@ gazelle_binary(
[
gazelle_generation_test(
name = file[0:-len("/WORKSPACE")],
env = {
"GAZELLE_JAVA_LOG_CALLER": "false",
"GAZELLE_JAVA_LOG_FORMAT": "json",
"GAZELLE_JAVA_LOG_TIMESTAMP": "false",
},
gazelle_binary = ":gazelle",
gazelle_timeout_seconds = 30,
test_data = glob(
Expand Down
2 changes: 1 addition & 1 deletion java/gazelle/testdata/annotations/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/bin/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
8 changes: 4 additions & 4 deletions java/gazelle/testdata/custom_test_suffixes/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
12:00AM WRN java/gazelle/resolve.go:XXX > Unable to find package for import in any dependency from rule=//src/test/com/example/child/grandchild package=org.junit
12:00AM WRN java/gazelle/resolve.go:XXX > Unable to find package for import in any dependency from rule=//src/test/com/example/child package=org.junit
12:00AM WRN java/gazelle/resolve.go:XXX > Unable to find package for import in any dependency from rule=//src/test/com/example package=org.junit
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
{"level":"warn","package":"org.junit","from rule":"//src/test/com/example/child/grandchild","message":"Unable to find package for import in any dependency"}
{"level":"warn","package":"org.junit","from rule":"//src/test/com/example/child","message":"Unable to find package for import in any dependency"}
{"level":"warn","package":"org.junit","from rule":"//src/test/com/example","message":"Unable to find package for import in any dependency"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/directive_exclude/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/grpc/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/inner_classes/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/interface_exports/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/lib/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
12:00AM FTL java/gazelle/generate.go:XXX > label parse error error="label parse error: repository has invalid characters: \"@invalid:label\"" dep=@invalid:label file.Pkg=src/main/java/com/example/hello name=hello
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
{"level":"fatal","file.Pkg":"src/main/java/com/example/hello","name":"hello","dep":"@invalid:label","error":"label parse error: repository has invalid characters: \"@invalid:label\"","message":"label parse error"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_java//java:defs.bzl", "java_library")

java_library(
name = "hello",
srcs = ["Hello.java"],
visibility = ["//:__subpackages__"],
runtime_deps = ["@invalid:label"],
)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
10 changes: 5 additions & 5 deletions java/gazelle/testdata/maven_with_collision/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
12:00AM ERR java/gazelle/resolve.go:XXX > Append one of the following to BUILD.bazel:
12:00AM ERR java/gazelle/resolve.go:XXX > # gazelle:resolve java com.google.common.primitives @maven//:com_google_guava_guava
12:00AM ERR java/gazelle/resolve.go:XXX > # gazelle:resolve java com.google.common.primitives @maven//:com_google_guava_guava_sources
12:00AM WRN java/gazelle/resolve.go:XXX > Unable to find package for import in any dependency from rule=//src/main/java/com/example/myproject package=com.google.common.primitives
12:00AM FTL java/gazelle/lang.go:XXX > the java extension encontered errors that will create invalid build files
{"level":"error","message":"Append one of the following to BUILD.bazel:"}
{"level":"error","message":"# gazelle:resolve java com.google.common.primitives @maven//:com_google_guava_guava"}
{"level":"error","message":"# gazelle:resolve java com.google.common.primitives @maven//:com_google_guava_guava_sources"}
{"level":"warn","package":"com.google.common.primitives","from rule":"//src/main/java/com/example/myproject","message":"Unable to find package for import in any dependency"}
{"level":"fatal","message":"the java extension encontered errors that will create invalid build files"}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/packages/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/proto/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
2 changes: 1 addition & 1 deletion java/gazelle/testdata/stdlib/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12:00AM WRN java/gazelle/private/maven/resolver.go:XXX > not loading maven dependencies error="open %WORKSPACEPATH%/maven_install.json: no such file or directory" _c=maven-resolver
{"level":"warn","_c":"maven-resolver","error":"open %WORKSPACEPATH%/maven_install.json: no such file or directory","message":"not loading maven dependencies"}

0 comments on commit fa1fe87

Please sign in to comment.