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

logs: add the buffer logger to inspect logs during testing #18743

Closed
wants to merge 8 commits into from

Conversation

singuliere
Copy link
Contributor

When a test runs and the function under tests writes messages in the logs, there is no reliable way to inspect them. By adding the buffer logger at the beginning of a test and removing it at the end, all log messages written during the test are collected. It is then possible to obtain the buffer and inspect it to verify it contains the expected strings.

Although it is not a good practice to rely on log strings to assert the desired side effect of a function being tested, there are a number of functions in the Gitea code that have that behavior. In order for them to be refactored and improved to be more explicit in how they expose their side effects, the first step is to write a test for their current behavior without modifying the code. The second step is to refactor the function and run the same tests to ensure the behavior of the function was not unexpectedly modified during the refactor.

An example of such function is the extraction of the git SHA / Refs during migration.


forgefriends source

@singuliere singuliere added this to the 1.17.0 milestone Feb 12, 2022
@singuliere singuliere force-pushed the forgefriends-mr43 branch 3 times, most recently from 9d5b37c to e05b1e3 Compare February 12, 2022 17:04
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Feb 12, 2022

I proposed a new PR, the test code can be more simple:

func TestLogChecker(t *testing.T) {
	_ = log.NewLogger(1000, "console", "console", `{"level":"info","stacktracelevel":"NONE","stderr":true}`)

	lc, cleanup := NewLogChecker(log.DEFAULT)
	defer cleanup()

	lc.ExpectContains("First", "Third")

	log.Info("test")
	assert.Error(t, lc.Check())

	log.Info("First")
	assert.Error(t, lc.Check())

	log.Info("Second")
	assert.Error(t, lc.Check())

	log.Info("Third")
	assert.NoError(t, lc.Check())
}

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 12, 2022
@singuliere
Copy link
Contributor Author

Because of the matter explained in this forum post, this PR is withdrawn.

@singuliere singuliere closed this Feb 12, 2022
@singuliere singuliere reopened this Feb 12, 2022
@singuliere singuliere force-pushed the forgefriends-mr43 branch 3 times, most recently from 70c6c92 to 51ddc1f Compare February 12, 2022 20:44
@singuliere singuliere closed this Feb 12, 2022
@singuliere singuliere reopened this Feb 13, 2022
@singuliere
Copy link
Contributor Author

singuliere commented Feb 13, 2022

Resumed as explained in this forum post.

@singuliere
Copy link
Contributor Author

Obsoleted by #18752 closing.

@singuliere singuliere closed this Feb 25, 2022
@go-gitea go-gitea locked and limited conversation to collaborators Apr 28, 2022
@wxiaoguang wxiaoguang removed this from the 1.17.0 milestone Jun 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. type/testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants