|
| 1 | +package loggerbase |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func createLoggerForTest(t *testing.T) *BaseLogger { |
| 10 | + |
| 11 | + t.Helper() // Marks this function as a test helper, avoids cluttering test output |
| 12 | + |
| 13 | + logger := NewBaseLogger("TEST") |
| 14 | + return logger |
| 15 | +} |
| 16 | + |
| 17 | +// chdirTemp changes the current working directory to a temporary directory for the duration of the test. |
| 18 | +func chdirTemp(t *testing.T) string { |
| 19 | + t.Helper() |
| 20 | + tmp := t.TempDir() |
| 21 | + old, err := os.Getwd() |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + if err := os.Chdir(tmp); err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + t.Cleanup(func() { _ = os.Chdir(old) }) |
| 29 | + return tmp |
| 30 | +} |
| 31 | + |
| 32 | +/******* |
| 33 | +* Start * |
| 34 | +*******/ |
| 35 | + |
| 36 | +func TestStart(t *testing.T) { |
| 37 | + |
| 38 | + _ = chdirTemp(t) // Change to a temporary directory |
| 39 | + |
| 40 | + logger := createLoggerForTest(t) |
| 41 | + |
| 42 | + // Sample data |
| 43 | + |
| 44 | + var startTime int64 |
| 45 | + |
| 46 | + t.Run("Start (first time)", func(t *testing.T) { |
| 47 | + |
| 48 | + // First start should succeed |
| 49 | + out := logger.Start() |
| 50 | + |
| 51 | + if out != nil { |
| 52 | + t.Fatalf("expected nil, got %v", out) |
| 53 | + } |
| 54 | + // Capture start time |
| 55 | + startTime = logger.StartTime |
| 56 | + |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("Start (not first time)", func(t *testing.T) { |
| 60 | + |
| 61 | + // El test Start (first time) debe haber tenido éxito y haber establecido startTime |
| 62 | + if startTime == 0 { |
| 63 | + t.Fatal("precondition failed: startTime should be set from the first start") |
| 64 | + } |
| 65 | + |
| 66 | + // Subsequent starts shouldn't change startTime |
| 67 | + out := logger.Start() |
| 68 | + if out != nil { |
| 69 | + t.Fatalf("expected nil, got %v", out) |
| 70 | + } |
| 71 | + if logger.StartTime != startTime { |
| 72 | + t.Fatalf("expected startTime to be %d, got %d", startTime, logger.StartTime) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +/************* |
| 79 | +* Create File * |
| 80 | +*************/ |
| 81 | + |
| 82 | +func TestCreateFile(t *testing.T) { |
| 83 | + |
| 84 | + _ = chdirTemp(t) // Change to a temporary directory |
| 85 | + logger := createLoggerForTest(t) |
| 86 | + |
| 87 | + t.Run("Create File (valid path)", func(t *testing.T) { |
| 88 | + filename := "logs/testlog.txt" |
| 89 | + file, err := logger.CreateFile(filename) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("expected nil error, got %v", err) |
| 92 | + } |
| 93 | + defer file.Close() |
| 94 | + |
| 95 | + // Verify file was created |
| 96 | + if _, err := os.Stat(filename); os.IsNotExist(err) { |
| 97 | + t.Fatalf("expected file %s to exist, but it does not", filename) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("Create File (invalid path)", func(t *testing.T) { |
| 102 | + // Using an invalid path (e.g., a path with illegal characters) |
| 103 | + invalidFilename := string([]byte{0}) + "invalidlog.txt" |
| 104 | + _, err := logger.CreateFile(invalidFilename) |
| 105 | + if err == nil { |
| 106 | + t.Fatal("expected an error for invalid path, got nil") |
| 107 | + } |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +/****** |
| 112 | +* Stop * |
| 113 | +******/ |
| 114 | +func TestStop(t *testing.T) { |
| 115 | + |
| 116 | + _ = chdirTemp(t) // Change to a temporary directory |
| 117 | + logger := createLoggerForTest(t) |
| 118 | + |
| 119 | + // Sample stop function |
| 120 | + sampleStopFunc := func() error { |
| 121 | + // Simulate some stop actions |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + t.Run("Stop (not started)", func(t *testing.T) { |
| 126 | + |
| 127 | + // Stopping without starting should be a no-op |
| 128 | + out := logger.Stop(sampleStopFunc) |
| 129 | + if out != nil { |
| 130 | + t.Fatalf("expected nil, got %v", out) |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("Stop (after start)", func(t *testing.T) { |
| 135 | + |
| 136 | + // Start the logger first |
| 137 | + _ = logger.Start() |
| 138 | + |
| 139 | + // Now stop the logger |
| 140 | + out := logger.Stop(sampleStopFunc) |
| 141 | + if out != nil { |
| 142 | + t.Fatalf("expected nil, got %v", out) |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + t.Run("Stop (already stopped)", func(t *testing.T) { |
| 147 | + |
| 148 | + // Stopping again should be a no-op |
| 149 | + out := logger.Stop(sampleStopFunc) |
| 150 | + if out != nil { |
| 151 | + t.Fatalf("expected nil, got %v", out) |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + // With a function that returns an error |
| 156 | + errorStopFunc := func() error { |
| 157 | + return os.ErrInvalid |
| 158 | + } |
| 159 | + |
| 160 | + t.Run("Stop (with error)", func(t *testing.T) { |
| 161 | + |
| 162 | + // Start the logger first |
| 163 | + _ = logger.Start() |
| 164 | + |
| 165 | + // Now stop the logger with an error-producing function |
| 166 | + out := logger.Stop(errorStopFunc) |
| 167 | + if out != os.ErrInvalid { |
| 168 | + t.Fatalf("expected os.ErrInvalid, got %v", out) |
| 169 | + } |
| 170 | + }) |
| 171 | + |
| 172 | +} |
0 commit comments