Skip to content

Commit

Permalink
Merge pull request #1165 from github/fix-flaky-test
Browse files Browse the repository at this point in the history
Fix flaky tests
  • Loading branch information
ArinGhazarian authored Nov 15, 2023
2 parents fd8187d + 04f4adc commit c1e1f9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public WaitForMigrationCommandHandlerTests()
{
WaitIntervalInSeconds = WAIT_INTERVAL
};

TestHelpers.SetCliContext();
}

[Fact]
Expand All @@ -54,7 +56,7 @@ public async Task With_Migration_ID_That_Succeeds_With_No_Warnings()
$"Migration {REPO_MIGRATION_ID} for {TARGET_REPO} is {RepositoryMigrationStatus.InProgress}",
$"Waiting {WAIT_INTERVAL} seconds...",
$"Migration {REPO_MIGRATION_ID} succeeded for {TARGET_REPO}",
$"Migration log available at {MIGRATION_URL} or by running `gh {CliContext.RootCommand} download-logs`"
$"Migration log available at {MIGRATION_URL} or by running `gh {TestHelpers.CLI_ROOT_COMMAND} download-logs`"
};

// Act
Expand Down Expand Up @@ -100,7 +102,7 @@ public async Task With_Migration_ID_That_Succeeds_With_1_Warning()
$"Waiting {WAIT_INTERVAL} seconds...",
$"Migration {REPO_MIGRATION_ID} succeeded for {TARGET_REPO}",
"1 warning encountered during this migration",
$"Migration log available at {MIGRATION_URL} or by running `gh {CliContext.RootCommand} download-logs`"
$"Migration log available at {MIGRATION_URL} or by running `gh {TestHelpers.CLI_ROOT_COMMAND} download-logs`"
};

// Act
Expand Down Expand Up @@ -147,7 +149,7 @@ public async Task With_Migration_ID_That_Succeeds_With_4_Warnings()
$"Waiting {WAIT_INTERVAL} seconds...",
$"Migration {REPO_MIGRATION_ID} succeeded for {TARGET_REPO}",
"4 warnings encountered during this migration",
$"Migration log available at {MIGRATION_URL} or by running `gh {CliContext.RootCommand} download-logs`"
$"Migration log available at {MIGRATION_URL} or by running `gh {TestHelpers.CLI_ROOT_COMMAND} download-logs`"
};

// Act
Expand Down Expand Up @@ -194,7 +196,7 @@ public async Task With_Migration_ID_That_Fails()
$"Migration {REPO_MIGRATION_ID} for {TARGET_REPO} is {RepositoryMigrationStatus.InProgress}",
$"Waiting {WAIT_INTERVAL} seconds...",
$"Migration {REPO_MIGRATION_ID} failed for {TARGET_REPO}",
$"Migration log available at {MIGRATION_URL} or by running `gh {CliContext.RootCommand} download-logs`"
$"Migration log available at {MIGRATION_URL} or by running `gh {TestHelpers.CLI_ROOT_COMMAND} download-logs`"
};

// Act
Expand Down Expand Up @@ -244,7 +246,7 @@ public async Task With_Migration_ID_That_Fails_Validation()
$"Migration {REPO_MIGRATION_ID} for {TARGET_REPO} is {RepositoryMigrationStatus.PendingValidation}",
$"Waiting {WAIT_INTERVAL} seconds...",
$"Migration {REPO_MIGRATION_ID} failed for {TARGET_REPO}",
$"Migration log available at {MIGRATION_URL} or by running `gh {CliContext.RootCommand} download-logs`"
$"Migration log available at {MIGRATION_URL} or by running `gh {TestHelpers.CLI_ROOT_COMMAND} download-logs`"
};

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ public class VersionCheckerTests
public void GetVersionComments_Returns_Root_And_Executing_Commands()
{
// Arrange
const string rootCommand = "ROOT_COMMAND";
const string executingCommand = "EXECUTING_COMMAND";

CliContext.RootCommand = rootCommand;
CliContext.ExecutingCommand = executingCommand;
TestHelpers.SetCliContext();

var versionChecker = new VersionChecker(null, null);

// Act
var comments = versionChecker.GetVersionComments();

// Assert
comments.Should().Be($"({rootCommand}/{executingCommand})");
comments.Should().Be($"({TestHelpers.CLI_ROOT_COMMAND}/{TestHelpers.CLI_EXECUTING_COMMAND})");
}
}
14 changes: 14 additions & 0 deletions src/OctoshiftCLI.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace OctoshiftCLI.Tests
{
public static class TestHelpers
{
private static readonly object _mutex = new();

public const string CLI_ROOT_COMMAND = "ROOT_COMMAND";
public const string CLI_EXECUTING_COMMAND = "EXECUTING_COMMAND";

public static Mock<T> CreateMock<T>() where T : class
{
var ctor = typeof(T).GetConstructors().First();
Expand All @@ -24,5 +29,14 @@ public static void VerifyCommandOption(IReadOnlyList<Option> options, string nam
Assert.Equal(required, option.IsRequired);
Assert.Equal(isHidden, option.IsHidden);
}

public static void SetCliContext()
{
lock (_mutex)
{
CliContext.RootCommand ??= CLI_ROOT_COMMAND;
CliContext.ExecutingCommand ??= CLI_EXECUTING_COMMAND;
}
}
}
}

0 comments on commit c1e1f9c

Please sign in to comment.