|
1 | 1 | using CodeIndex.Cli; |
2 | 2 | using CodeIndex.Models; |
| 3 | +using Microsoft.Data.Sqlite; |
3 | 4 | using System.Text.Json; |
4 | 5 |
|
5 | 6 | namespace CodeIndex.Tests; |
@@ -150,6 +151,88 @@ public void QueryQuietFlag_PreservesErrorLines() |
150 | 151 | Assert.DoesNotContain("Hint:", stderr); |
151 | 152 | } |
152 | 153 |
|
| 154 | + [Fact] |
| 155 | + public void Run_UnhandledExceptionReturnsUnhandledExitCode() |
| 156 | + { |
| 157 | + lock (TestConsoleLock.Gate) |
| 158 | + { |
| 159 | + var originalError = Console.Error; |
| 160 | + using var stderr = new StringWriter(); |
| 161 | + try |
| 162 | + { |
| 163 | + Console.SetError(stderr); |
| 164 | + |
| 165 | + var exitCode = ProgramRunner.Run( |
| 166 | + ["status"], |
| 167 | + appVersion: "1.0.0-test", |
| 168 | + beforeDispatchForTesting: () => throw new InvalidOperationException("boom")); |
| 169 | + |
| 170 | + Assert.Equal(CommandExitCodes.UnhandledException, exitCode); |
| 171 | + Assert.Contains("Error: command failed before it could complete.", stderr.ToString()); |
| 172 | + Assert.DoesNotContain("InvalidOperationException", stderr.ToString()); |
| 173 | + } |
| 174 | + finally |
| 175 | + { |
| 176 | + Console.SetError(originalError); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [Theory] |
| 182 | + [InlineData(5)] |
| 183 | + [InlineData(6)] |
| 184 | + [InlineData(8)] |
| 185 | + public void Run_UnhandledSqliteTransientExceptionReturnsTransientDatabaseExitCode(int sqliteErrorCode) |
| 186 | + { |
| 187 | + lock (TestConsoleLock.Gate) |
| 188 | + { |
| 189 | + var originalError = Console.Error; |
| 190 | + using var stderr = new StringWriter(); |
| 191 | + try |
| 192 | + { |
| 193 | + Console.SetError(stderr); |
| 194 | + |
| 195 | + var exitCode = ProgramRunner.Run( |
| 196 | + ["status"], |
| 197 | + appVersion: "1.0.0-test", |
| 198 | + beforeDispatchForTesting: () => throw new SqliteException("database unavailable", sqliteErrorCode)); |
| 199 | + |
| 200 | + Assert.Equal(CommandExitCodes.TransientDatabaseError, exitCode); |
| 201 | + Assert.Contains("Error: command failed before it could complete.", stderr.ToString()); |
| 202 | + } |
| 203 | + finally |
| 204 | + { |
| 205 | + Console.SetError(originalError); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + [Fact] |
| 211 | + public void Run_UnhandledPermanentSqliteExceptionReturnsDatabaseExitCode() |
| 212 | + { |
| 213 | + lock (TestConsoleLock.Gate) |
| 214 | + { |
| 215 | + var originalError = Console.Error; |
| 216 | + using var stderr = new StringWriter(); |
| 217 | + try |
| 218 | + { |
| 219 | + Console.SetError(stderr); |
| 220 | + |
| 221 | + var exitCode = ProgramRunner.Run( |
| 222 | + ["status"], |
| 223 | + appVersion: "1.0.0-test", |
| 224 | + beforeDispatchForTesting: () => throw new SqliteException("database disk image is malformed", 11)); |
| 225 | + |
| 226 | + Assert.Equal(CommandExitCodes.DatabaseError, exitCode); |
| 227 | + Assert.Contains("Error: command failed before it could complete.", stderr.ToString()); |
| 228 | + } |
| 229 | + finally |
| 230 | + { |
| 231 | + Console.SetError(originalError); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
153 | 236 | [Fact] |
154 | 237 | public void Completions_HelpLikeValueReturnsCompletionsError() |
155 | 238 | { |
|
0 commit comments