Skip to content

Commit

Permalink
Lock when initializing template repo (#766)
Browse files Browse the repository at this point in the history
* Lock when initializing template repo

* Refresh TemplateRepo, fix build and cleanup

* Fix parallel test failures
  • Loading branch information
myieye authored May 8, 2024
1 parent 98d48e3 commit 6869617
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
29 changes: 17 additions & 12 deletions backend/Testing/Fixtures/IntegrationFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using LexCore.Utils;
using Shouldly;
Expand All @@ -13,11 +11,15 @@ namespace Testing.Fixtures;

public class IntegrationFixture : IAsyncLifetime
{
private static readonly string _templateRepoName = "test-template-repo.zip";
public FileInfo TemplateRepoZip { get; } = new(_templateRepoName);
public DirectoryInfo TemplateRepo { get; } = new(Path.Join(BasePath, "_template-repo_"));
private const string TemplateRepoZipName = "test-template-repo.zip";
public static readonly FileInfo TemplateRepoZip = new(TemplateRepoZipName);
public static readonly DirectoryInfo TemplateRepo = new(Path.Join(BasePath, "_template-repo_"));
public ApiTestBase AdminApiTester { get; private set; } = new();
private string AdminJwt = string.Empty;

static IntegrationFixture()
{
DeletePreviousTestFiles();
}

public async Task InitializeAsync(ApiTestBase apiTester)
{
Expand All @@ -27,10 +29,9 @@ public async Task InitializeAsync(ApiTestBase apiTester)

public async Task InitializeAsync()
{
DeletePreviousTestFiles();
Directory.CreateDirectory(BasePath);
InitTemplateRepo();
AdminJwt = await AdminApiTester.LoginAs(AdminAuth.Username, AdminAuth.Password);
await AdminApiTester.LoginAs(AdminAuth.Username, AdminAuth.Password);
}

public Task DisposeAsync()
Expand All @@ -43,11 +44,15 @@ private static void DeletePreviousTestFiles()
if (Directory.Exists(BasePath)) Directory.Delete(BasePath, true);
}

private void InitTemplateRepo()
private static void InitTemplateRepo()
{
if (TemplateRepo.Exists) return;
using var stream = TemplateRepoZip.OpenRead();
ZipFile.ExtractToDirectory(stream, TemplateRepo.FullName);
lock (TemplateRepo)
{
if (TemplateRepo.Exists) return;
using var stream = TemplateRepoZip.OpenRead();
ZipFile.ExtractToDirectory(stream, TemplateRepo.FullName);
TemplateRepo.Refresh();
}
}

public ProjectConfig InitLocalFlexProjectWithRepo(HgProtocol? protocol = null, [CallerMemberName] string projectName = "")
Expand Down
26 changes: 6 additions & 20 deletions backend/Testing/Fixtures/IntegrationFixtureTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Moq;
using Shouldly;
using Testing.ApiTests;
using Testing.Services;

namespace Testing.Fixtures;

Expand All @@ -10,46 +9,33 @@ namespace Testing.Fixtures;
/// </summary>
public class IntegrationFixtureTests
{
// A static shared instance for the whole test class, because that's how xunit uses fixtures
private static readonly IntegrationFixture fixture = new();

[Fact]
public async Task InitCreatesARepoWithTheProject()
{
var fixture = new IntegrationFixture();
await fixture.InitializeAsync(Mock.Of<ApiTestBase>());
fixture.TemplateRepo.EnumerateFiles()
IntegrationFixture.TemplateRepo.EnumerateFiles()
.Select(f => f.Name)
.ShouldContain("kevin-test-01.fwdata");
}

[Fact]
public async Task CanFindTheProjectZipFile()
{
var fixture = new IntegrationFixture();
await fixture.InitializeAsync(Mock.Of<ApiTestBase>());
fixture.TemplateRepoZip
IntegrationFixture.TemplateRepoZip
.Directory!.EnumerateFiles().Select(f => f.Name)
.ShouldContain(fixture.TemplateRepoZip.Name);
.ShouldContain(IntegrationFixture.TemplateRepoZip.Name);
}

[Fact]
public async Task CanInitFlexProjectRepo()
{
var fixture = new IntegrationFixture();
await fixture.InitializeAsync(Mock.Of<ApiTestBase>());
var projectConfig = fixture.InitLocalFlexProjectWithRepo();
Directory.EnumerateFiles(projectConfig.Dir)
.ShouldContain(projectConfig.FwDataFile);
}

[Fact]
public async Task InitCleansUpPreviousRun()
{
var fixture = new IntegrationFixture();
await fixture.InitializeAsync(Mock.Of<ApiTestBase>());
var filePath = Path.Join(Constants.BasePath, "test.txt");
await File.WriteAllTextAsync(filePath, "test");
Directory.EnumerateFiles(Constants.BasePath).ShouldContain(filePath);

await fixture.InitializeAsync();
Directory.EnumerateFiles(Constants.BasePath).ShouldNotContain(filePath);
}
}

0 comments on commit 6869617

Please sign in to comment.