Skip to content

Commit

Permalink
GitDb BlogSettingsConfigurationProvider #131 (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
petervandenhout authored Sep 6, 2021
1 parent c52ab4d commit b964fa3
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 6 deletions.
10 changes: 10 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ steps:
versioningScheme: byEnvVar
versionEnvVar: 'GitVersion.NuGetVersion'

- task: DotNetCoreCLI@2
displayName: NuGet Pack Opw.PineBlog.GitDb
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
inputs:
command: pack
packagesToPack: '**/Opw.PineBlog.GitDb.csproj'
nobuild: true
versioningScheme: byEnvVar
versionEnvVar: 'GitVersion.NuGetVersion'

- task: DotNetCoreCLI@2
displayName: NuGet Pack Opw.PineBlog
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
Expand Down
2 changes: 1 addition & 1 deletion samples/Opw.PineBlog.Sample.GitDb/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"RepositoryUrl": "https://github.com/ofpinewood/pineblog-gitdb.git",
"Branch": "test",
"RootPath": "pineblog",
"SyncFrequency": 300
"SyncFrequency": 180
},
"Logging": {
"LogLevel": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Opw.PineBlog.GitDb
{
// TODO: add tests
/// <summary>
/// Provides blog settings configuration key/values for the application.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/Opw.PineBlog.GitDb/GitDbSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ private void Sync(object state)
var branchesSynced = context.Sync();
if (branchesSynced < 1)
_logger.LogError($"Could not sync repository \"{_options.Value.RepositoryUrl}\".");

CheckBlogSettingsFileChanges(context);
}
}
catch (Exception ex)
Expand All @@ -60,6 +62,21 @@ private void Sync(object state)
_logger.LogInformation($"GitDbSyncService: \"{_options.Value.RepositoryUrl}\" synced.");
}

private void CheckBlogSettingsFileChanges(GitDbContext context)
{
var path = PathHelper.Build(_options.Value.LocalRepositoryBasePath, _options.Value.RootPath, GitDbConstants.BlogSettingsFile);
if (!File.Exists(path))
return;

var lastModifiedDate = File.GetLastWriteTimeUtc(path);
if (lastModifiedDate != null && lastModifiedDate > DateTime.UtcNow.AddSeconds(-10))
{
path = PathHelper.Build(_options.Value.RootPath, GitDbConstants.BlogSettingsFile);
var files = context.GetFiles(new string[] { path });
FileChangeObserver.Instance.OnChanged(new FileChangeEventArgs(GitDbConstants.BlogSettingsFile, files[path]));
}
}

public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("GitDbSyncService: Hosted Service is stopping.");
Expand Down
2 changes: 2 additions & 0 deletions src/Opw.PineBlog.GitDb/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Opw.PineBlog.GitDb.Tests")]
2 changes: 1 addition & 1 deletion src/Opw.PineBlog.GitDb/Opw.PineBlog.GitDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>PineBlog data provider that uses a Git repository.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>Opw.PineBlog.Git</PackageId>
<PackageTags>blog git</PackageTags>
<PackageTags>blog gitdb git</PackageTags>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Opw.PineBlog.GitDb/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Opw.PineBlog.GitDb
{
// TODO: add tests
internal static class PathHelper
{
internal static string Build(params string[] parts)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using FluentAssertions;
using Microsoft.Extensions.Options;

namespace Opw.PineBlog.GitDb
{
public class BlogSettingsConfigurationProviderTests : GitDbTestsBase
{
private readonly IOptions<PineBlogGitDbOptions> _options;
private readonly BlogSettingsConfigurationProvider _provider;

public BlogSettingsConfigurationProviderTests(GitDbFixture fixture) : base(fixture)
{
_options = ServiceProvider.GetRequiredService<IOptions<PineBlogGitDbOptions>>();

_provider = new BlogSettingsConfigurationProvider(new BlogSettingsConfigurationSource
{
Options = _options.Value,
ReloadOnChange = true
});
}

[Fact(Skip = Constants.SkipGitDbBlogSettingsConfigurationProviderTests)]
public void Load_Should_HaveSettings()
{
_provider.Load();

_provider.TryGet($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.Title)}", out var title);
_provider.TryGet($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.Description)}", out var description);

title.Should().Be("PineBlog");
description.Should().Be("A blogging engine based on ASP.NET Core MVC Razor Pages and Entity Framework Core");
}

[Fact(Skip = Constants.SkipGitDbBlogSettingsConfigurationProviderTests)]
public void Load_Should_NotHaveSettings()
{
var options = _options.Value;
options.RootPath = "invalid";

var provider = new BlogSettingsConfigurationProvider(new BlogSettingsConfigurationSource
{
Options = _options.Value,
ReloadOnChange = true
});

provider.Load();

provider.TryGet($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.Title)}", out var title);

title.Should().BeNull();
}
}
}
8 changes: 8 additions & 0 deletions tests/Opw.PineBlog.GitDb.Tests/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace Opw.PineBlog.GitDb
{
public static class Constants
{
public const string SkipGitDbBlogSettingsConfigurationProviderTests = null;//"These tests do not work on the build server.";
}
}
21 changes: 19 additions & 2 deletions tests/Opw.PineBlog.GitDb.Tests/LibGit2/GitDbContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@ namespace Opw.PineBlog.GitDb.LibGit2
{
public class GitDbContextTests : GitDbTestsBase
{
private readonly IOptions<PineBlogGitDbOptions> _options;
private readonly GitDbContext _gitDbContext;

public GitDbContextTests(GitDbFixture fixture) : base(fixture)
{
var options = ServiceProvider.GetService<IOptions<PineBlogGitDbOptions>>();
_gitDbContext = GitDbContext.Create(options.Value);
_options = ServiceProvider.GetService<IOptions<PineBlogGitDbOptions>>();
_gitDbContext = GitDbContext.Create(_options.Value);
}

[Fact]
public void Create_Default_GitDbContext()
{
var gitDbContext = GitDbContext.Create(_options.Value);

gitDbContext.Should().NotBeNull();
}

[Fact]
public void CreateFromLocal_Default_GitDbContext()
{
var gitDbContext = GitDbContext.CreateFromLocal(_options.Value);

gitDbContext.Should().NotBeNull();
}

[Fact]
Expand Down
32 changes: 32 additions & 0 deletions tests/Opw.PineBlog.GitDb.Tests/PathHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using FluentAssertions;
using Xunit;

namespace Opw.PineBlog.GitDb
{
public class PathHelperTests
{
[Fact]
public void Build_Should_ReturnPath()
{
var result = PathHelper.Build("A", "B", "C");

result.Should().Be("A/B/C");
}

[Fact]
public void Build_Should_TrimSlashes()
{
var result = PathHelper.Build("/A", "\\B", "C");

result.Should().Be("A/B/C");
}

[Fact]
public void Build_Should_IgnoreWhiteSpaceParts()
{
var result = PathHelper.Build("A", " ", "", null, "B", "C");

result.Should().Be("A/B/C");
}
}
}

0 comments on commit b964fa3

Please sign in to comment.