test: isolates temp test fixtures (#1569)
## Motivation and Context Replace shared temp paths with `t.TempDir()`` so each test gets its own cleanup-scoped directory. - This avoids leftover files and cross-test interference. No production code changed. ## How Has This Been Tested? GitHub CI passed. - https://github.com/yowainwright/registry/pull/1 ## Breaking Changes No breaking changes; just tests. ## Types of changes - [x] chore ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed ## Additional context Small test-only cleanup. No production code paths are changed. Co-authored-by: Radoslav Dimitrov <radoslav@stacklok.com>
This commit is contained in:
parent
068a97a2f8
commit
dd4b2939e3
3 changed files with 16 additions and 33 deletions
|
|
@ -95,9 +95,7 @@ func SetupTestToken(t *testing.T, registryURL, token string) string {
|
|||
func CreateTestServerJSON(t *testing.T, serverJSON apiv0.ServerJSON) (string, string) {
|
||||
t.Helper()
|
||||
|
||||
tempDir, err := os.MkdirTemp("", "mcp-publisher-test")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { os.RemoveAll(tempDir) })
|
||||
tempDir := t.TempDir()
|
||||
|
||||
jsonData, err := json.MarshalIndent(serverJSON, "", " ")
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
|
|
@ -140,16 +140,13 @@ func TestValidateCommand_NoServerFile(t *testing.T) {
|
|||
server := SetupMockRegistryServer(t, nil, nil)
|
||||
SetupTestToken(t, server.URL, "test-token")
|
||||
|
||||
// Don't create server.json
|
||||
tempDir, err := os.MkdirTemp("", "mcp-publisher-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
tempDir := t.TempDir()
|
||||
|
||||
originalDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
defer func() { _ = os.Chdir(originalDir) }()
|
||||
|
||||
_ = os.Chdir(tempDir)
|
||||
require.NoError(t, os.Chdir(tempDir))
|
||||
|
||||
err = commands.ValidateCommand([]string{})
|
||||
|
||||
|
|
@ -161,15 +158,13 @@ func TestValidateCommand_InvalidJSON(t *testing.T) {
|
|||
server := SetupMockRegistryServer(t, nil, nil)
|
||||
SetupTestToken(t, server.URL, "test-token")
|
||||
|
||||
tempDir, err := os.MkdirTemp("", "mcp-publisher-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
tempDir := t.TempDir()
|
||||
|
||||
originalDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
defer func() { _ = os.Chdir(originalDir) }()
|
||||
|
||||
_ = os.Chdir(tempDir)
|
||||
require.NoError(t, os.Chdir(tempDir))
|
||||
|
||||
// Create invalid JSON file
|
||||
err = os.WriteFile("server.json", []byte("{ invalid json }"), 0600)
|
||||
|
|
|
|||
|
|
@ -20,8 +20,7 @@ import (
|
|||
)
|
||||
|
||||
func TestImportService_LocalFile(t *testing.T) {
|
||||
// Create a temporary seed file
|
||||
tempFile := filepath.Join(os.TempDir(), "test_import_seed.json")
|
||||
tempFile := filepath.Join(t.TempDir(), "test_import_seed.json")
|
||||
seedData := []*apiv0.ServerJSON{
|
||||
{
|
||||
Schema: model.CurrentSchemaURL,
|
||||
|
|
@ -41,7 +40,6 @@ func TestImportService_LocalFile(t *testing.T) {
|
|||
|
||||
err = os.WriteFile(tempFile, jsonData, 0600)
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tempFile)
|
||||
|
||||
// Create registry service
|
||||
testDB := database.NewTestDB(t)
|
||||
|
|
@ -187,6 +185,14 @@ func TestImportService_ErrorHandling(t *testing.T) {
|
|||
registryService := service.NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
|
||||
importerService := importer.NewService(registryService)
|
||||
|
||||
errorDir := t.TempDir()
|
||||
missingFile := filepath.Join(errorDir, "non-existent-file.json")
|
||||
invalidFile := filepath.Join(errorDir, "invalid.json")
|
||||
|
||||
invalidJSON := []byte("{invalid json}")
|
||||
err := os.WriteFile(invalidFile, invalidJSON, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
|
|
@ -195,13 +201,13 @@ func TestImportService_ErrorHandling(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
name: "non-existent local file",
|
||||
path: "/tmp/non-existent-file.json",
|
||||
path: missingFile,
|
||||
expectError: true,
|
||||
errorMsg: "failed to read seed data",
|
||||
},
|
||||
{
|
||||
name: "invalid JSON file",
|
||||
path: "/tmp/invalid.json",
|
||||
path: invalidFile,
|
||||
expectError: true,
|
||||
errorMsg: "failed to read seed data",
|
||||
},
|
||||
|
|
@ -213,22 +219,6 @@ func TestImportService_ErrorHandling(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
// Create invalid JSON file for testing
|
||||
invalidJSON := []byte("{invalid json}")
|
||||
tempFile, err := os.CreateTemp("", "invalid-*.json")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tempFile.Name())
|
||||
err = os.WriteFile(tempFile.Name(), invalidJSON, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Update test case to use temp file
|
||||
for i := range tests {
|
||||
if tests[i].path == "/tmp/invalid.json" {
|
||||
tests[i].path = tempFile.Name()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := importerService.ImportFromPath(context.Background(), tt.path)
|
||||
|
|
|
|||
Loading…
Reference in a new issue