Skip to content

Commit addca5b

Browse files
authored
chore: Add ExecResult serializable test data (#1456)
1 parent 8588794 commit addca5b

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

src/Testcontainers/Containers/DockerContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ await _configuration.StartupCallback(this, ct)
520520

521521
Logger.CompleteReadinessCheck(_container.ID);
522522

523-
StartedTime = DateTime.TryParse(_container.State.StartedAt, null, DateTimeStyles.AdjustToUniversal, out var startedTime) ? startedTime : DateTime.UtcNow;
523+
StartedTime = DateTime.TryParse(_container.State.StartedAt, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var startedTime) ? startedTime : DateTime.UtcNow;
524524
Started?.Invoke(this, EventArgs.Empty);
525525
}
526526

@@ -556,7 +556,7 @@ await _client.StopAsync(_container.ID, ct)
556556
_container = new ContainerInspectResponse();
557557
}
558558

559-
StoppedTime = DateTime.TryParse(_container.State.FinishedAt, null, DateTimeStyles.AdjustToUniversal, out var stoppedTime) ? stoppedTime : DateTime.UtcNow;
559+
StoppedTime = DateTime.TryParse(_container.State.FinishedAt, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var stoppedTime) ? stoppedTime : DateTime.UtcNow;
560560
Stopped?.Invoke(this, EventArgs.Empty);
561561
}
562562

tests/Testcontainers.Platform.Linux.Tests/ExecFailedExceptionTest.cs

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,91 @@ namespace Testcontainers.Tests;
22

33
public sealed class ExecFailedExceptionTest
44
{
5-
public static readonly List<TheoryDataRow<ExecResult, string>> ExecResultTestData
6-
= new List<TheoryDataRow<ExecResult, string>>
5+
public static TheoryData<ExecResultSerializable, string> ExecResultTestData { get; }
6+
= new TheoryData<ExecResultSerializable, string>
77
{
8-
new TheoryDataRow<ExecResult, string>
9-
(
10-
new ExecResult("Stdout\nStdout", "Stderr\nStderr", 1),
8+
{
9+
new ExecResultSerializable("Stdout\nStdout", "Stderr\nStderr", 1),
1110
"Process exited with code 1." + Environment.NewLine +
1211
" Stdout: " + Environment.NewLine +
1312
" Stdout" + Environment.NewLine +
1413
" Stdout" + Environment.NewLine +
1514
" Stderr: " + Environment.NewLine +
1615
" Stderr" + Environment.NewLine +
1716
" Stderr"
18-
),
19-
new TheoryDataRow<ExecResult, string>
20-
(
21-
new ExecResult("Stdout\nStdout", string.Empty, 1),
17+
},
18+
{
19+
new ExecResultSerializable("Stdout\nStdout", string.Empty, 1),
2220
"Process exited with code 1." + Environment.NewLine +
2321
" Stdout: " + Environment.NewLine +
2422
" Stdout" + Environment.NewLine +
2523
" Stdout"
26-
),
27-
new TheoryDataRow<ExecResult, string>
28-
(
29-
new ExecResult(string.Empty, "Stderr\nStderr", 1),
24+
},
25+
{
26+
new ExecResultSerializable(string.Empty, "Stderr\nStderr", 1),
3027
"Process exited with code 1." + Environment.NewLine +
3128
" Stderr: " + Environment.NewLine +
3229
" Stderr" + Environment.NewLine +
3330
" Stderr"
34-
),
35-
new TheoryDataRow<ExecResult, string>
36-
(
37-
new ExecResult(string.Empty, string.Empty, 1),
31+
},
32+
{
33+
new ExecResultSerializable(string.Empty, string.Empty, 1),
3834
"Process exited with code 1."
39-
),
35+
},
4036
};
4137

4238
[Theory]
4339
[MemberData(nameof(ExecResultTestData))]
44-
public void ExecFailedExceptionCreatesExpectedMessage(ExecResult execResult, string message)
40+
public void ExecFailedExceptionCreatesExpectedMessage(ExecResultSerializable serializable, string message)
4541
{
42+
// Given
43+
var execResult = serializable.ToExecResult();
44+
45+
// When
4646
var exception = new ExecFailedException(execResult);
47+
48+
// Then
4749
Assert.Equal(execResult, exception.ExecResult);
4850
Assert.Equal(message, exception.Message);
4951
}
52+
53+
[PublicAPI]
54+
public sealed class ExecResultSerializable : IXunitSerializable
55+
{
56+
private string _stdout;
57+
58+
private string _stderr;
59+
60+
private int _exitCode;
61+
62+
public ExecResultSerializable()
63+
{
64+
}
65+
66+
public ExecResultSerializable(string stdout, string stderr, int exitCode)
67+
{
68+
_stdout = stdout;
69+
_stderr = stderr;
70+
_exitCode = exitCode;
71+
}
72+
73+
public ExecResult ToExecResult()
74+
{
75+
return new ExecResult(_stdout, _stderr, _exitCode);
76+
}
77+
78+
public void Deserialize(IXunitSerializationInfo info)
79+
{
80+
_stdout = info.GetValue<string>(nameof(_stdout));
81+
_stderr = info.GetValue<string>(nameof(_stderr));
82+
_exitCode = info.GetValue<int>(nameof(_exitCode));
83+
}
84+
85+
public void Serialize(IXunitSerializationInfo info)
86+
{
87+
info.AddValue(nameof(_stdout), _stdout);
88+
info.AddValue(nameof(_stderr), _stderr);
89+
info.AddValue(nameof(_exitCode), _exitCode);
90+
}
91+
}
5092
}

tests/Testcontainers.Platform.Linux.Tests/Usings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
global using Microsoft.Extensions.Logging.Abstractions;
2424
global using Microsoft.Extensions.Logging.Testing;
2525
global using ReflectionMagic;
26-
global using Xunit;
26+
global using Xunit;
27+
global using Xunit.Sdk;

0 commit comments

Comments
 (0)