Skip to content

Test file handler #146

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

Merged
merged 5 commits into from
May 16, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
test: use t.TempDir() for temporary directories
  • Loading branch information
DNEGEL3125 committed May 16, 2025
commit d0c719152614b5704a1620c3ada39a7d191fb896
30 changes: 7 additions & 23 deletions cmd/go-judge/rest_executor/file_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import (

func TestFilePost(t *testing.T) {
// Create a temporary directory for the file store
tempDir, err := os.MkdirTemp("", "test_storage")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
tempDir := t.TempDir()

// Initialize the file store
router := gin.Default()
Expand Down Expand Up @@ -100,11 +96,7 @@ func CreateFileWithContent(filePath, content string) error {
// TestFileGet tests the file retrieval functionality
func TestFileGet(t *testing.T) {
// Create a temporary directory for the file store
tempDir, err := os.MkdirTemp("", "test_storage")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
tempDir := t.TempDir()

// Initialize the file store
router := gin.Default()
Expand All @@ -125,7 +117,7 @@ func TestFileGet(t *testing.T) {
// Create files in the temporary directory
for _, file := range filesToCreate {
filePath := path.Join(tempDir, file.Name)
err = CreateFileWithContent(filePath, file.Content)
err := CreateFileWithContent(filePath, file.Content)
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
Expand Down Expand Up @@ -157,11 +149,7 @@ func TestFileGet(t *testing.T) {
// TestFileIDGet tests the file retrieval by ID functionality
func TestFileIDGet(t *testing.T) {
// Create a temporary directory for the file store
tempDir, err := os.MkdirTemp("", "test_storage")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
tempDir := t.TempDir()

// Initialize the file store
router := gin.Default()
Expand All @@ -171,7 +159,7 @@ func TestFileIDGet(t *testing.T) {
// Create a test file
testFileName := "test.py"
testFilePath := path.Join(tempDir, testFileName)
err = CreateFileWithContent(testFilePath, "print(58 - 7 * 3)")
err := CreateFileWithContent(testFilePath, "print(58 - 7 * 3)")
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
Expand Down Expand Up @@ -209,11 +197,7 @@ func TestFileIDGet(t *testing.T) {
// TestFileIDDelete tests the file deletion functionality
func TestFileIDDelete(t *testing.T) {
// Create a temporary directory for the file store
tempDir, err := os.MkdirTemp("", "test_storage")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
tempDir := t.TempDir()

// Initialize the file store
router := gin.Default()
Expand All @@ -223,7 +207,7 @@ func TestFileIDDelete(t *testing.T) {
// Create a test file
testFileName := "test.py"
testFilePath := path.Join(tempDir, testFileName)
err = CreateFileWithContent(testFilePath, "print(58 - 7 * 3)")
err := CreateFileWithContent(testFilePath, "print(58 - 7 * 3)")
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
Expand Down