Skip to content

Commit 971ca17

Browse files
AndyButlandCopilot
andauthored
Add unit test verifying dockerfile aligns with current target framework (#19445)
* Add unit test verifying dockerfile aligns with current target framework. * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent d2a0cba commit 971ca17

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Umbraco.
2+
// See LICENSE for more details.
3+
4+
using System.Reflection;
5+
using System.Runtime.Versioning;
6+
using NUnit.Framework;
7+
8+
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Website;
9+
10+
[TestFixture]
11+
public class DockerFileTests
12+
{
13+
[Test]
14+
public void DockerFile_AspNet_Version_Matches_Target_Framework()
15+
{
16+
var targetFrameworkVersion = GetNetVersionFromCurrentTargetFramework();
17+
(string dockerFileAspNetVersion, string dockerFileSdkVersion) = GetNetVersionsFromDockerFile();
18+
Assert.AreEqual(dockerFileAspNetVersion, dockerFileSdkVersion);
19+
Assert.AreEqual(targetFrameworkVersion, dockerFileAspNetVersion);
20+
}
21+
22+
private static string GetNetVersionFromCurrentTargetFramework()
23+
{
24+
var targetFrameworkAttribute = Assembly.GetExecutingAssembly()
25+
.GetCustomAttributes(typeof(TargetFrameworkAttribute), false)
26+
.SingleOrDefault() as TargetFrameworkAttribute;
27+
Assert.IsNotNull(targetFrameworkAttribute);
28+
29+
return targetFrameworkAttribute.FrameworkName.Replace(".NETCoreApp,Version=v", string.Empty);
30+
}
31+
32+
private static (string DockerFileAspNetVersion, string DockerFileSdkVersion) GetNetVersionsFromDockerFile()
33+
{
34+
const int SegmentsToRepoRoot = 5; // Number of directory segments from the test directory to the repository root.
35+
var testContextDirectoryParts = TestContext.CurrentContext.TestDirectory.Split(Path.DirectorySeparatorChar);
36+
var solutionRootDirectory = string.Join(Path.DirectorySeparatorChar, testContextDirectoryParts.Take(testContextDirectoryParts.Length - SegmentsToRepoRoot));
37+
var dockerFilePath = Path.Combine(solutionRootDirectory, "templates", "UmbracoProject", "Dockerfile");
38+
39+
var dockerFileContent = File.ReadAllText(dockerFilePath);
40+
var dockerFileFromLines = dockerFileContent
41+
.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
42+
.Where(x => x.StartsWith("FROM mcr."))
43+
.ToList();
44+
45+
Assert.AreEqual(2, dockerFileFromLines.Count);
46+
47+
var dockerFileAspNetVersion = GetVersionFromDockerFromLine(dockerFileFromLines[0]);
48+
var dockerFileSdkVersion = GetVersionFromDockerFromLine(dockerFileFromLines[1]);
49+
50+
return (dockerFileAspNetVersion, dockerFileSdkVersion);
51+
}
52+
53+
private static string GetVersionFromDockerFromLine(string line) => line.Split(' ')[1].Split(':')[1];
54+
}

0 commit comments

Comments
 (0)