Question: Getting segfault with PrepareTestDatabase when creating new module #6154

Closed
opened 2025-11-02 06:46:32 -06:00 by GiteaMirror · 1 comment
Owner

Originally created by @kasbah on GitHub (Oct 15, 2020).

I am trying to create a new module called myrepo to learn how to extend gitea. I wanted to start off with a test:

// modules/myrepo/create_test.go

package myrepo

import (
	"testing"

	"code.gitea.io/gitea/models"

	"github.com/stretchr/testify/assert"
)

func TestMyRepo(t *testing.T) {
	// this is fine
	assert.Equal(t, 1, 1)
	// this gives the segmentation violation
	assert.NoError(t, models.PrepareTestDatabase())
}

I do:

go test  -mod=vendor -tags='sqlite sqlite_unlock_notify' code.gitea.io/gitea/modules/myrepo

The error:

--- FAIL: TestMyRepo (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x12433c6]

goroutine 13 [running]:
testing.tRunner.func1.1(0x14acae0, 0x271c630)
	/usr/lib/go/src/testing/testing.go:1076 +0x30d
testing.tRunner.func1(0xc00047a900)
	/usr/lib/go/src/testing/testing.go:1079 +0x41a
panic(0x14acae0, 0x271c630)
	/usr/lib/go/src/runtime/panic.go:969 +0x175
github.com/go-testfixtures/testfixtures/v3.(*Loader).Load(0x0, 0x0, 0x0)
	/home/kaspar/projects/kitspace/gitea/vendor/github.com/go-testfixtures/testfixtures/v3/testfixtures.go:336 +0x26
code.gitea.io/gitea/models.LoadFixtures(0x1a586a0, 0xc00047a900)
	/home/kaspar/projects/kitspace/gitea/models/test_fixtures.go:60 +0x88
code.gitea.io/gitea/models.PrepareTestDatabase(...)
	/home/kaspar/projects/kitspace/gitea/models/unit_tests.go:120
code.gitea.io/gitea/modules/myrepo.TestMyRepo(0xc00047a900)
	/home/kaspar/projects/kitspace/gitea/modules/myrepo/create_test.go:19 +0x7a
testing.tRunner(0xc00047a900, 0x1810888)
	/usr/lib/go/src/testing/testing.go:1127 +0xef
created by testing.(*T).Run
	/usr/lib/go/src/testing/testing.go:1178 +0x386
FAIL	code.gitea.io/gitea/modules/myrepo	0.040s
FAIL

Any idea why this happens? (If it's better to pose dev questions like this on the forum, let me know.)

Originally created by @kasbah on GitHub (Oct 15, 2020). I am trying to create a new module called `myrepo` to learn how to extend gitea. I wanted to start off with a test: ```go // modules/myrepo/create_test.go package myrepo import ( "testing" "code.gitea.io/gitea/models" "github.com/stretchr/testify/assert" ) func TestMyRepo(t *testing.T) { // this is fine assert.Equal(t, 1, 1) // this gives the segmentation violation assert.NoError(t, models.PrepareTestDatabase()) } ``` I do: ``` go test -mod=vendor -tags='sqlite sqlite_unlock_notify' code.gitea.io/gitea/modules/myrepo ``` The error: ``` --- FAIL: TestMyRepo (0.00s) panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x12433c6] goroutine 13 [running]: testing.tRunner.func1.1(0x14acae0, 0x271c630) /usr/lib/go/src/testing/testing.go:1076 +0x30d testing.tRunner.func1(0xc00047a900) /usr/lib/go/src/testing/testing.go:1079 +0x41a panic(0x14acae0, 0x271c630) /usr/lib/go/src/runtime/panic.go:969 +0x175 github.com/go-testfixtures/testfixtures/v3.(*Loader).Load(0x0, 0x0, 0x0) /home/kaspar/projects/kitspace/gitea/vendor/github.com/go-testfixtures/testfixtures/v3/testfixtures.go:336 +0x26 code.gitea.io/gitea/models.LoadFixtures(0x1a586a0, 0xc00047a900) /home/kaspar/projects/kitspace/gitea/models/test_fixtures.go:60 +0x88 code.gitea.io/gitea/models.PrepareTestDatabase(...) /home/kaspar/projects/kitspace/gitea/models/unit_tests.go:120 code.gitea.io/gitea/modules/myrepo.TestMyRepo(0xc00047a900) /home/kaspar/projects/kitspace/gitea/modules/myrepo/create_test.go:19 +0x7a testing.tRunner(0xc00047a900, 0x1810888) /usr/lib/go/src/testing/testing.go:1127 +0xef created by testing.(*T).Run /usr/lib/go/src/testing/testing.go:1178 +0x386 FAIL code.gitea.io/gitea/modules/myrepo 0.040s FAIL ``` Any idea why this happens? (If it's better to pose dev questions like this on the forum, let me know.)
Author
Owner

@kasbah commented on GitHub (Oct 15, 2020):

I found out I need to add this to modules/myrepo/create_test.go:

import (
	"path/filepath"
      // ...
)

func TestMain(m *testing.M) {
	models.MainTest(m, filepath.Join("..", ".."))
}
@kasbah commented on GitHub (Oct 15, 2020): I found out I need to add this to `modules/myrepo/create_test.go`: ```go import ( "path/filepath" // ... ) func TestMain(m *testing.M) { models.MainTest(m, filepath.Join("..", "..")) } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#6154