Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the build number in the About window #105

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Teams Status Pub Changelog

## Unreleased

- Added CI build number to version reported in About window ([#105](https://github.com/tetsuo13/TeamsStatusPub/pull/105))

## 1.4.0 (2023-11-30)

- .NET 8 ([#91](https://github.com/tetsuo13/TeamsStatusPub/pull/91))
Expand Down
25 changes: 22 additions & 3 deletions src/TeamsStatusPub/Services/AssemblyAppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ public string Version
get
{
var version = _assembly.GetName().Version;
return $"Version {version?.Major}.{version?.Minor}.{version?.Build}";

// This should never happen.
if (version is null)
{
return "Unknown";
}

var canonicalVersion = $"Version {version.Major}.{version.Minor}.{version.Build}";

// CI build will set this with the build number.
if (version.Revision != -1)
{
canonicalVersion += $" (Build {version.Revision})";
}

return canonicalVersion;
}
}

Expand All @@ -25,9 +40,13 @@ public string Version
/// <summary>
/// Initializes a new instance of the AssemblyAppInfo class.
/// </summary>
public AssemblyAppInfo()
public AssemblyAppInfo() : this(Assembly.GetExecutingAssembly())
{
}

public AssemblyAppInfo(Assembly assembly)
{
_assembly = Assembly.GetExecutingAssembly();
_assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
}

private string GetAssemblyAttribute<T>(Func<T, string> attributeProperty)
Expand Down
47 changes: 40 additions & 7 deletions tests/TeamsStatusPub.UnitTests/Services/AppInfoTests.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,66 @@
using TeamsStatusPub.Services;
using System;
using System.Reflection;
using NSubstitute;
using TeamsStatusPub.Services;
using Xunit;

namespace TeamsStatusPub.UnitTests.Services;

public static class AppInfoTests
{
private static AssemblyAppInfo appInfo => new();
/// <summary>
/// Generic instance that will pull values from the TeamsStatusPub project
/// assembly. Create a more specialized instance for the test method
/// to mock <see cref="Assembly"/> for the
/// <see cref="AssemblyAppInfo(Assembly)"/> constructor.
/// </summary>
private static AssemblyAppInfo _appInfo => new();

[Fact]
public static void ApplicationName_FromAssembly()
{
Assert.Equal("Teams Status Pub", appInfo.ApplicationName);
Assert.Equal("Teams Status Pub", _appInfo.ApplicationName);
}

[Fact]
public static void Copyright_FromAssembly()
{
Assert.StartsWith("Copyright © 20", appInfo.Copyright);
Assert.StartsWith("Copyright © 20", _appInfo.Copyright);
}

[Fact]
public static void WebsiteUrl_FromAssembly()
{
Assert.StartsWith("https://www.github.com/tetsuo13", appInfo.WebsiteUrl);
Assert.StartsWith("https://www.github.com/tetsuo13", _appInfo.WebsiteUrl);
}

[Theory]
[InlineData("1.2.3", "Version 1.2.3")]
[InlineData("1.2.3.4", "Version 1.2.3 (Build 4)")]
public static void Version_FromAssembly(string assemblyVersion, string expected)
{
var appInfo = GetAppInfoForVersion(assemblyVersion);
Assert.Equal(expected, appInfo.Version);
}

[Fact]
public static void Version_FromAssembly()
public static void Version_Null_FromAssembly()
{
var appInfo = GetAppInfoForVersion(null);
Assert.Equal("Unknown", appInfo.Version);
}

private static AssemblyAppInfo GetAppInfoForVersion(string? assemblyVersion)
{
Assert.Matches(@"Version \d+\.\d+\.\d+", appInfo.Version);
var assemblyName = new AssemblyName();

if (assemblyVersion is not null)
{
assemblyName.Version = new Version(assemblyVersion);
}

var assemblyMock = Substitute.For<Assembly>();
assemblyMock.GetName().Returns(assemblyName);
return new(assemblyMock);
}
}