This repository was archived by the owner on Sep 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil_test.go
More file actions
104 lines (88 loc) · 2.88 KB
/
util_test.go
File metadata and controls
104 lines (88 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/go-logr/stdr"
mg "github.com/hypermodeinc/modusgraph"
"github.com/stretchr/testify/require"
)
// CreateTestClient creates a new ModusGraph client for testing purposes with a configured logger.
// It returns the client and a cleanup function that should be deferred by the caller.
func CreateTestClient(t *testing.T, uri string) (mg.Client, func()) {
stdLogger := log.New(os.Stdout, "", log.LstdFlags)
logger := stdr.NewWithOptions(stdLogger, stdr.Options{LogCaller: stdr.All}).WithName("mg")
verbosity := os.Getenv("MODUSGRAPH_TEST_LOG_LEVEL")
if verbosity == "" {
stdr.SetVerbosity(0)
} else {
level, err := strconv.Atoi(verbosity)
if err != nil {
stdr.SetVerbosity(0)
} else {
stdr.SetVerbosity(level)
}
}
client, err := mg.NewClient(uri, mg.WithAutoSchema(true), mg.WithLogger(logger))
require.NoError(t, err)
cleanup := func() {
err := client.DropAll(context.Background())
if err != nil {
t.Error(err)
}
client.Close()
// Properly shutdown the engine and reset the singleton state
mg.Shutdown()
}
return client, cleanup
}
// GetTempDir returns a temporary directory for testing purposes.
// It creates a unique directory for each test and registers a cleanup function to remove it.
// On Windows, it uses the standard temp directory and creates a unique directory for each test.
// On other platforms, it uses the standard toolchain TempDir function.
func GetTempDir(t *testing.T) string {
if runtime.GOOS == "windows" {
baseDir := os.TempDir()
testName := t.Name()
testName = strings.ReplaceAll(testName, "/", "_")
testName = strings.ReplaceAll(testName, "\\", "_")
testName = strings.ReplaceAll(testName, ":", "_")
tempDir := filepath.Join(baseDir, "modusgraph_test_"+testName)
err := os.MkdirAll(tempDir, 0755)
if err != nil {
t.Logf("Failed to create temp directory %s: %v, falling back to standard temp dir", tempDir, err)
return os.TempDir()
}
t.Cleanup(func() {
runtime.GC()
time.Sleep(200 * time.Millisecond)
if err := os.RemoveAll(tempDir); err != nil {
t.Logf("Warning: failed to remove temp directory %s: %v", tempDir, err)
}
})
return tempDir
}
return t.TempDir()
}
// SetupTestEnv configures the environment variables for tests.
// This is particularly useful when debugging tests in an IDE.
func SetupTestEnv(logLevel int) {
// Only set these if they're not already set in the environment
if os.Getenv("MODUSGRAPH_TEST_ADDR") == "" {
os.Setenv("MODUSGRAPH_TEST_ADDR", "localhost:9080")
}
if os.Getenv("MODUSGRAPH_TEST_LOG_LEVEL") == "" {
// Uncomment to enable verbose logging during debugging
os.Setenv("MODUSGRAPH_TEST_LOG_LEVEL", strconv.Itoa(logLevel))
}
}