Skip to content

Commit

Permalink
[dotnet/templates/tests] Update template code. (#12109)
Browse files Browse the repository at this point in the history
* Move the templates to a common subdirectory.
* Add .NET unit test to verify that all the templates build and that they have
  no warnings.
* Modify the template code to:
   * Use the UIApplication.Main overload that takes a Type.
   * Use top-level statements for the Main method to simplify code.
   * Remove a few unnecessary usings.

Partial fix for #12085.
  • Loading branch information
rolfbjarne authored Jul 14, 2021
1 parent e0e0a71 commit 7ac3417
Show file tree
Hide file tree
Showing 101 changed files with 148 additions and 57 deletions.
13 changes: 0 additions & 13 deletions dotnet/Microsoft.MacCatalyst.Templates/maccatalyst/Main.cs

This file was deleted.

13 changes: 0 additions & 13 deletions dotnet/Microsoft.iOS.Templates/ios/Main.cs

This file was deleted.

11 changes: 0 additions & 11 deletions dotnet/Microsoft.macOS.Templates/macos/Main.cs

This file was deleted.

13 changes: 0 additions & 13 deletions dotnet/Microsoft.tvOS.Templates/tvos/Main.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UIKit;

using MacCatalystApp1;

// This is the main entry point of the application.
// If you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, typeof (AppDelegate));
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Foundation;
using UIKit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using CoreFoundation;
using Foundation;
using UIKit;

Expand Down
8 changes: 8 additions & 0 deletions dotnet/Templates/Microsoft.iOS.Templates/ios/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UIKit;

using iOSApp1;

This comment has been minimized.

Copy link
@mhutch

mhutch Jul 20, 2021

Member

Why does this need a using for its own namespace?

This comment has been minimized.

Copy link
@rolfbjarne

rolfbjarne Jul 21, 2021

Author Member

It wouldn't compile otherwise, the compiler said it couldn't find the AppDelegate type used below.

This comment has been minimized.

Copy link
@mhutch

mhutch Jul 21, 2021

Member

That's unfortunate. Maybe DefaultNamespace should be in the default global usings? /cc @DamianEdwards

This comment has been minimized.

Copy link
@DamianEdwards

DamianEdwards Jul 21, 2021

Interesting, in the web templates the app namespace is kept as an explicit using in places where the file needs it, which is admittedly not often, but also isn't usually the default namespace but rather a derivative, e.g. Company.ThisApp.Data, so not sure whether this (needing the default namespace) is just a special case or one that will be hit a lot. Here's an example of what I'm referring to.

This comment has been minimized.

Copy link
@mhutch

mhutch Jul 22, 2021

Member

I guess that's doable here too, but it feels like it makes toplevel statements more verbose than the body of a main method was?


// This is the main entry point of the application.
// If you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, typeof (AppDelegate));
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Foundation;
using UIKit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace iOSLib1 {
public class Class1 {
}
Expand Down
7 changes: 7 additions & 0 deletions dotnet/Templates/Microsoft.macOS.Templates/macos/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using AppKit;

using macOSApp1;

// This is the main entry point of the application.
NSApplication.Init ();
NSApplication.Main (args);
8 changes: 8 additions & 0 deletions dotnet/Templates/Microsoft.tvOS.Templates/tvos/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UIKit;

using tvOSApp1;

// This is the main entry point of the application.
// If you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, typeof (AppDelegate));
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Foundation;
using UIKit;

namespace tvOSApp1 {
Expand Down
2 changes: 1 addition & 1 deletion dotnet/package/microsoft.templates.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PackageId>Microsoft.$(_PlatformName).Templates</PackageId>
<PackageType>Template</PackageType>
<Description>Templates for $(_PlatformName) platforms</Description>
<_packagePath>$(MSBuildThisFileDirectory)..\Microsoft.$(_PlatformName).Templates\</_packagePath>
<_packagePath>$(MSBuildThisFileDirectory)..\Templates\Microsoft.$(_PlatformName).Templates\</_packagePath>
</PropertyGroup>
<Import Project="common.csproj" />
</Project>
5 changes: 5 additions & 0 deletions tests/common/BinLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public static IEnumerable<string> PrintToLines (string path)
}
}

public static IEnumerable<BuildLogEvent> GetBuildLogWarnings (string path)
{
return GetBuildMessages (path).Where (v => v.Type == BuildLogEventType.Warning);
}

public static IEnumerable<BuildLogEvent> GetBuildMessages (string path)
{
var reader = new BinLogReader ();
Expand Down
25 changes: 25 additions & 0 deletions tests/common/DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ public static ExecutionResult AssertBuildFailure (string project, Dictionary<str
return rv;
}

public static ExecutionResult AssertNew (string outputDirectory, string template)
{
Directory.CreateDirectory (outputDirectory);

var args = new List<string> ();
args.Add ("new");
args.Add (template);

var env = new Dictionary<string, string> ();
env ["MSBuildSDKsPath"] = null;
env ["MSBUILD_EXE_PATH"] = null;
var output = new StringBuilder ();
var rv = Execution.RunWithStringBuildersAsync (Executable, args, env, output, output, Console.Out, workingDirectory: outputDirectory, timeout: TimeSpan.FromMinutes (10)).Result;
if (rv.ExitCode != 0) {
Console.WriteLine ($"'{Executable} {StringUtils.FormatArguments (args)}' failed with exit code {rv.ExitCode}.");
Console.WriteLine (output);
Assert.AreEqual (0, rv.ExitCode, $"Exit code: {Executable} {StringUtils.FormatArguments (args)}");
}
return new ExecutionResult {
StandardOutput = output,
StandardError = output,
ExitCode = rv.ExitCode,
};
}

public static ExecutionResult Execute (string verb, string project, Dictionary<string, string> properties, bool assert_success = true)
{
if (!File.Exists (project))
Expand Down
4 changes: 4 additions & 0 deletions tests/dotnet/UnitTests/DotNetUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
<PackageReference Include="MSBuild.StructuredLogger" Version="2.1.500" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -51,4 +52,7 @@
<ItemGroup>
<Folder Include="external\" />
</ItemGroup>
<ItemGroup>
<None Remove="System.Text.Json" />
</ItemGroup>
</Project>
82 changes: 82 additions & 0 deletions tests/dotnet/UnitTests/TemplateTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

using NUnit.Framework;

using Xamarin.Utils;

namespace Xamarin.Tests {
[TestFixture]
public class TemplateTest {

public static string [][] Templates = {
// { platform, template_name }
new [] { "iOS", "ios" },
new [] { "iOS", "ioslib" },
new [] { "tvOS", "tvos" },
new [] { "MacCatalyst", "maccatalyst" },
new [] { "macOS", "macos" },
};

public class TemplateConfig {
public string Name;
public string ShortName;
public TemplateConfigTag Tags;
}

public class TemplateConfigTag {
public string Language;
public string Type;
}

[Test]
public void AreAllTemplatesListed ()
{
var allListedTemplates = Templates.Select (v => v [1]).ToArray ();
var allTemplates = new List<string> ();
foreach (var platform in Enum.GetValues<ApplePlatform> ()) {
var dir = Path.Combine (Configuration.SourceRoot, "dotnet", "Templates", $"Microsoft.{platform.AsString ()}.Templates");
if (!Directory.Exists (dir))
continue;

var templateDirectories = Directory.GetDirectories (dir);
var options = new JsonSerializerOptions {
PropertyNameCaseInsensitive = true,
IncludeFields = true,
};

// read the template's configuration to figure out if it's a project template, and if not, skip it
foreach (var templateDir in templateDirectories) {
var jsonPath = Path.Combine (templateDir, ".template.config", "template.json");
if (!File.Exists (jsonPath))
continue;
var json = JsonSerializer.Deserialize<TemplateConfig> (File.ReadAllText (jsonPath), options);
if (json.Tags.Type != "project")
continue;

allTemplates.Add (json.ShortName);
}
}
Assert.That (allListedTemplates, Is.EquivalentTo (allTemplates), "The listed templates here and the templates on disk don't match");
}

[Test]
[TestCaseSource (nameof (Templates))]
public void CreateAndBuildTemplate (string platform, string template)
{
Configuration.IgnoreIfIgnoredPlatform (platform);
var tmpDir = Cache.CreateTemporaryDirectory ();
Configuration.CopyDotNetSupportingFiles (tmpDir);
var outputDir = Path.Combine (tmpDir, template);
DotNet.AssertNew (outputDir, template);
var csproj = Path.Combine (outputDir, template + ".csproj");
var rv = DotNet.AssertBuild (csproj);
var warnings = BinLog.GetBuildLogWarnings (rv.BinLogPath).Select (v => v.Message);
Assert.That (warnings, Is.Empty, $"Build warnings:\n\t{string.Join ("\n\t", warnings)}");
}
}
}

8 comments on commit 7ac3417

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ [CI Build] Tests failed on Build ❌

Tests failed on Build.

API diff

✅ API Diff from stable

View API diff

API & Generator diff

API Diff (from PR only) (no change)
Generator Diff (no change)

Packages generated

View packages

Test results

2 tests failed, 219 tests passed.

Failed tests

  • framework-test/Mac Catalyst/Debug: TimedOut (Execution timed out after 1200 seconds.
    No test log file was produced)
  • DotNet tests: Failed (Execution failed with exit code 1)

Pipeline on Agent XAMBOT-1037.BigSur'
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Tests failed catastrophically on VSTS: device tests tvOS 🔥

Not enough free space in the host.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests tvOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests iOS). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Tests passed on macOS Mac Catalina (10.15) ✅

Tests passed

All tests on macOS X Mac Catalina (10.15) passed.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Tests passed on macOS Mac Mojave (10.14) ✅

Tests passed

All tests on macOS X Mac Mojave (10.14) passed.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Tests passed on macOS Mac High Sierra (10.13) ✅

Tests passed

All tests on macOS X Mac High Sierra (10.13) passed.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Tests were not ran (VSTS: device tests iOS32b). ⚠️

Results were skipped for this run due to provisioning problems Azure Devops. Please contact the bot administrator.

Pipeline on Agent
[dotnet/templates/tests] Update template code. (#12109)

Please sign in to comment.