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

Extend Templates testing framework to test through API #5677

Merged
merged 6 commits into from
Dec 13, 2022

Conversation

JanKrivanek
Copy link
Member

Problem

We are missing ability to leverage Templates Testing Tooling from within the Template Engine repository
This might be needed during moving samples: #2522

Solution

Extend TemplateVerifier to allow for custom instantiation callback
Add separate extension implementation performing instantiation through TemplateCreator API (not included within TemplateVerifier project to minimize dependecy pull for external scenarios)

Checks:

  • Added unit tests
  • Added #nullable enable to all the modified files ?

@JanKrivanek JanKrivanek requested a review from a team as a code owner November 25, 2022 18:19
UniqueFor = UniqueForOption.Architecture,
}
.WithInstantiationThroughTemplateCreatorApi(new Dictionary<string, string?>() { { "paramB", "true" } })
Copy link
Member Author

Choose a reason for hiding this comment

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

@YuliiaKovalova this can be inspiration how the imported sample templates might be tested (only templateName, TemplatePath and WithInstantiationThroughTemplateCreatorApi will be needed - rest is here to unit test the actual testing functionality)

Copy link
Member

@vlada-shubina vlada-shubina left a comment

Choose a reason for hiding this comment

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

Please consider using IDE.Bootstrapper or full e2e via Edge for installing, loading and running the template.
The current logic likely deviates too much from regular experience, also doesn't support all the cases.

@@ -18,7 +19,10 @@ public sealed class DefaultEnvironment : IEnvironment
private const int DefaultBufferWidth = 160;
private readonly IReadOnlyDictionary<string, string> _environmentVariables;

public DefaultEnvironment()
public DefaultEnvironment() : this(null)
Copy link
Member

Choose a reason for hiding this comment

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

just a thought - would it be better to do own implementation? Maybe something as ExtendedEnvironment?

another thought: https://github.com/dotnet/sdk/blob/main/src/Cli/Microsoft.TemplateEngine.Cli/CliEnvironment.cs we can get rid of this custom implementation with this class now.

Copy link
Member Author

Choose a reason for hiding this comment

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

There were few options:

  • Unseal the DefaultEnvironment and extend.
  • Copy paste DefaultEnvironment logic and add custom logic
  • Add the logic into DefaultEnvironment

The last one felt as the one with least dept incured. Though I might miss some obvious reason why it might not be an optional choice - so I'm open to alternative thoughts :-)

Copy link
Member

Choose a reason for hiding this comment

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

My preference is the first one, just to intentionally keep it separate, so it is not misused, however I don't insist.

Comment on lines +11 to +25
/// <summary>
/// Exit code of the action (e.g. exit code of the dotnet new command).
/// 0 indicates successful action. Nonzero otherwise.
/// </summary>
int ExitCode { get; }

/// <summary>
/// Standard output stream content for the instantiation action.
/// </summary>
string StdOut { get; }

/// <summary>
/// Standard error stream content for the instantiation action.
/// </summary>
string StdErr { get; }
Copy link
Member

Choose a reason for hiding this comment

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

just a thought - does it make sense to be in an a separate interface?
IInstantiationResult
ICLIInstantiationResult : IInstantiationResult

@vlada-shubina
Copy link
Member

Example using Bootstrapper is here:

using Bootstrapper bootstrapper = GetBootstrapper();
string templateLocation = GetTestTemplateLocation("SourceNameForms");
await InstallTemplateAsync(bootstrapper, templateLocation).ConfigureAwait(false);
string output = TestUtils.CreateTemporaryFolder();
var foundTemplates = await bootstrapper.GetTemplatesAsync(new[] { WellKnownSearchFilters.NameFilter("TestAssets.SourceNameForms") }).ConfigureAwait(false);
var result = await bootstrapper.CreateAsync(foundTemplates[0].Info, "MyApp.1", output, new Dictionary<string, string?>()).ConfigureAwait(false);

Copy link
Member

@vlada-shubina vlada-shubina left a comment

Choose a reason for hiding this comment

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

Half way through, left some comments in, mostly typos.

src/Microsoft.TemplateEngine.IDE/Bootstrapper.cs Outdated Show resolved Hide resolved
/// <returns><see cref="ITemplateCreationResult"/> containing information on created template or error occurred.</returns>
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
public Task<ITemplateCreationResult> CreateAsync(
Copy link
Member

Choose a reason for hiding this comment

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

just a thought - should we make other one obsolete?
also we are hardly using baselines, not sure if we should keep it and leave the warning.

Copy link
Member Author

Choose a reason for hiding this comment

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

The other CreateAsync (with params as dictionary) has legitimate use for quick testing with set of params - I'd keep it as is

src/Microsoft.TemplateEngine.Edge/DefaultEnvironment.cs Outdated Show resolved Hide resolved
src/Microsoft.TemplateEngine.Edge/DefaultEnvironment.cs Outdated Show resolved Hide resolved
@@ -18,7 +19,10 @@ public sealed class DefaultEnvironment : IEnvironment
private const int DefaultBufferWidth = 160;
private readonly IReadOnlyDictionary<string, string> _environmentVariables;

public DefaultEnvironment()
public DefaultEnvironment() : this(null)
Copy link
Member

Choose a reason for hiding this comment

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

My preference is the first one, just to intentionally keep it separate, so it is not misused, however I don't insist.

Copy link
Member

@vlada-shubina vlada-shubina left a comment

Choose a reason for hiding this comment

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

Completed review - looks good, couple of minor comments and maybe some possible improvements for future.

Comment on lines +121 to +122
result.Status == CreationResultStatus.Success ? string.Format(LocalizableStrings.CreateSuccessful, result.TemplateFullName) : string.Empty,
result.ErrorMessage ?? string.Empty,
Copy link
Member

Choose a reason for hiding this comment

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

Possible improvement: redirect logging here (info - output, error + warning - error).

@JanKrivanek JanKrivanek merged commit 04c1ccf into dotnet:main Dec 13, 2022
@JanKrivanek JanKrivanek deleted the te-api-testing branch December 13, 2022 15:05
@JanKrivanek
Copy link
Member Author

/backport to release/7.0.2xx

@github-actions
Copy link
Contributor

Started backporting to release/7.0.2xx: https://github.com/dotnet/templating/actions/runs/3686675295

@github-actions
Copy link
Contributor

@JanKrivanek backporting to release/7.0.2xx failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Extend Templates testing framework to test through API
Applying: Reuse bootstrapper class
Applying: Bugfix after merge
error: sha1 information is lacking or useless (src/Microsoft.TemplateEngine.Edge/PublicAPI.Unshipped.txt).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0003 Bugfix after merge
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@github-actions
Copy link
Contributor

@JanKrivanek an error occurred while backporting to release/7.0.2xx, please check the run log for details!

Error: git am failed, most likely due to a merge conflict.

JanKrivanek added a commit to JanKrivanek/templating that referenced this pull request Dec 13, 2022
* Extend Templates testing framework to test through API

* Reuse bootstrapper class

* Bugfix after merge

* Reflect PR comments

* Add forgotten changes
@JanKrivanek
Copy link
Member Author

/backport to release/7.0.2xx

@github-actions
Copy link
Contributor

Started backporting to release/7.0.2xx: https://github.com/dotnet/templating/actions/runs/3697375728

@github-actions
Copy link
Contributor

@JanKrivanek backporting to release/7.0.2xx failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Extend Templates testing framework to test through API
.git/rebase-apply/patch:296: trailing whitespace.
    
.git/rebase-apply/patch:297: trailing whitespace.
    
.git/rebase-apply/patch:309: trailing whitespace.
        
.git/rebase-apply/patch:311: trailing whitespace.
        
.git/rebase-apply/patch:313: trailing whitespace.
        
warning: squelched 36 whitespace errors
warning: 41 lines add whitespace errors.
Using index info to reconstruct a base tree...
M	tools/Microsoft.TemplateEngine.Authoring.TemplateVerifier/TemplateVerifierOptions.cs
M	tools/Microsoft.TemplateEngine.Authoring.TemplateVerifier/VerificationEngine.cs
Falling back to patching base and 3-way merge...
Auto-merging tools/Microsoft.TemplateEngine.Authoring.TemplateVerifier/VerificationEngine.cs
CONFLICT (content): Merge conflict in tools/Microsoft.TemplateEngine.Authoring.TemplateVerifier/VerificationEngine.cs
Auto-merging tools/Microsoft.TemplateEngine.Authoring.TemplateVerifier/TemplateVerifierOptions.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Extend Templates testing framework to test through API
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@github-actions
Copy link
Contributor

@JanKrivanek an error occurred while backporting to release/7.0.2xx, please check the run log for details!

Error: git am failed, most likely due to a merge conflict.

JanKrivanek added a commit to JanKrivanek/templating that referenced this pull request Dec 14, 2022
* Extend Templates testing framework to test through API

* Reuse bootstrapper class

* Bugfix after merge

* Reflect PR comments

* Add forgotten changes
JanKrivanek added a commit that referenced this pull request Dec 20, 2022
…to release/7.0.2xx (#5766)

* Extend Templates testing framework to test through API (#5677)

* Extend Templates testing framework to test through API

* Reuse bootstrapper class

* Bugfix after merge

* Reflect PR comments

* Add forgotten changes

* Fix contracts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants