Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/publisher/commands/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions cmd/publisher/commands/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand All @@ -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)
Expand Down
32 changes: 11 additions & 21 deletions internal/importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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",
},
Expand All @@ -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)
Expand Down
Loading