Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Unit test failure using Go 1.21.0 #3

Open
lpar opened this issue Aug 17, 2023 · 1 comment
Open

bug: Unit test failure using Go 1.21.0 #3

lpar opened this issue Aug 17, 2023 · 1 comment

Comments

@lpar
Copy link

lpar commented Aug 17, 2023

Description

Description

When running go test with Go 1.21.0, there's a unit test failure.

Expected Behavior

PASS
ok  	github.com/gobuffalo/mw-i18n/v2	0.281s

Actual Behavior

--- FAIL: Test_i18n_Localized_View (0.00s)
    i18n_test.go:165: 
        	Error Trace:	/Users/meta/Go/src/github.com/gobuffalo/mw-i18n/i18n_test.go:165
        	Error:      	Not equal: 
        	            	expected: "Default"
        	            	actual  : "Hello!"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-Default
        	            	+Hello!
        	Test:       	Test_i18n_Localized_View

To Reproduce

Installed via rtx:

rtx install [email protected]
rtx install [email protected]

Test:

rtx use [email protected]
go test # succeeds
rtx use [email protected]
go test # fails

Additional Context

macOS on Apple M2 Max.

@lpar
Copy link
Author

lpar commented Aug 18, 2023

I think I've tracked down what's going on here. The unit test Test_i18n_Localized_View calls app() to set up the buffalo.App. That does:

// Setup and use translations:
t, err := i18n.New(os.DirFS("locales"), "en-US")
if err != nil {
	log.Fatal(err)
}
// Setup URL prefix Language extractor
t.LanguageExtractors = append(t.LanguageExtractors, i18n.URLPrefixLanguageExtractor)

app.Use(t.Middleware())
// …chunk of code omitted
noI18n := func(c buffalo.Context) error {
	return c.Render(200, r.HTML("localized_view.html"))
}
app.Middleware.Skip(t.Middleware(), noI18n)

The problem is, t.Middleware() returns an inline function:

func (t *Translator) Middleware() buffalo.MiddlewareFunc {
	return func(next buffalo.Handler) buffalo.Handler {
//…chunk of code omitted
	}
}

So each call to t.Middleware() returns a different instance of the inline function. Apparently this wasn't the case in previous versions of Go.

So then when MiddlewareStack.handler checks the list in ms.skips, the funcKey value doesn't match between the instance added to the skip list and the instance added to the router.

If the unit test is revised to use a single instance of the translator middleware function:

// Setup and use translations:
t, err := i18n.New(os.DirFS("locales"), "en-US")
if err != nil {
	log.Fatal(err)
}
translatorMiddleware := t.Middleware()
// Setup URL prefix Language extractor
t.LanguageExtractors = append(t.LanguageExtractors, i18n.URLPrefixLanguageExtractor)

app.Use(translatorMiddleware)
// …chunk of code omitted
// Disable i18n middleware
noI18n := func(c buffalo.Context) error {
	return c.Render(200, r.HTML("localized_view.html"))
}
app.Middleware.Skip(translatorMiddleware, noI18n)

...then the test passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant