Skip to content

Tool to generate format.ps1xml #5666

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

Merged
merged 11 commits into from
Mar 30, 2018
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
3 changes: 3 additions & 0 deletions TestMappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,8 @@
],
"tools/BuildPackagesTask/": [
".\\tools\\BuildPackagesTask\\Microsoft.Azure.Build.Tasks.Test\\bin\\Debug\\Microsoft.Azure.Build.Tasks.Test.dll"
],
"tools/RepoTasks/": [
".\\tools\\RepoTasks\\RepoTasks.Cmdlets.Tests\\bin\\Debug\\RepoTasks.Cmdlets.Tests.dll"
]
}
7 changes: 7 additions & 0 deletions build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

<StaticAnalysis Include=".\tools\StaticAnalysis\StaticAnalysis.sln" />
<LocalBuildTasks Include="$(LibraryToolsFolder)\BuildPackagesTask\Microsoft.Azure.Build.Tasks.sln" />
<LocalBuildTasks Include="$(LibraryToolsFolder)\RepoTasks\RepoTasks.sln" />
</ItemGroup>

<!-- Tasks -->
Expand Down Expand Up @@ -148,6 +149,7 @@
<NuGetRestoreConfigSwitch>-ConfigFile &quot;$(NuGetRestoreConfigFile)&quot;</NuGetRestoreConfigSwitch>
<NuGetRestorePackageSetting>-PackagesDirectory $(LibraryNugetPackageFolder)</NuGetRestorePackageSetting>
<NuGetLocalBuildTaskPackages>-PackagesDirectory $(LibraryToolsFolder)\BuildPackagesTask\packages</NuGetLocalBuildTaskPackages>
<NuGetRepoTasksPackages>-PackagesDirectory $(LibraryToolsFolder)\RepoTasks\packages</NuGetRepoTasksPackages>

<PowerShellCommand Condition=" '$(PowerShellCommand)' == '' ">C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellCommand>
<PowerShellCoreCommand Condition=" '$(PowerShellCoreCommand)' == '' ">pwsh</PowerShellCoreCommand>
Expand Down Expand Up @@ -182,6 +184,11 @@
<!-- Restore packages for local build tasks-->
<Exec Command="$(NuGetCommand) restore %(LocalBuildTasks.FullPath) $(NuGetRestoreConfigSwitch) $(NuGetLocalBuildTaskPackages)"
ContinueOnError="false" />
<!-- Restore packages for repo tasks tasks-->
<Exec Command="$(NuGetCommand) restore %(LocalBuildTasks.FullPath) $(NuGetRestoreConfigSwitch) $(NuGetRepoTasksPackages)"
ContinueOnError="false" />
<Exec Command="$(NuGetCommand) restore %(SetupSln.FullPath) $(NuGetRestoreConfigSwitch) $(NuGetRestorePackageSetting)"
ContinueOnError="false" />

<!--Restore the xunit runner needed to run unit tests-->
<Exec Command="$(NuGetCommand) restore $(MSBuildProjectDirectory)\packages.config -PackagesDirectory $(MSBuildProjectDirectory)\packages" />
Expand Down
33 changes: 33 additions & 0 deletions src/Common/Commands.Common/Attributes/Ps1XmlAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;

namespace Microsoft.WindowsAzure.Commands.Common.Attributes
{
[Flags]
public enum ViewControl
{
None = 0,
Table,
List,
All = Table | List,
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class Ps1XmlAttribute : Attribute
{
public string Label { get; set; }

public ViewControl Target { get; set; } = ViewControl.Table;
}
}
1 change: 1 addition & 0 deletions src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\Ps1XmlAttribute.cs" />
<Compile Include="LongRunningJobCancelledException.cs" />
<Compile Include="ShouldMethodInvoker.cs" />
<Compile Include="AzureLongRunningJob.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.WindowsAzure.Commands.Common.Attributes;

namespace Microsoft.Azure.Commands.Profile.Models
{
Expand Down Expand Up @@ -86,19 +87,16 @@ public PSAzureSubscription(PSObject other)
this.PopulateExtensions(other);
}

/// <summary>
/// The subscription id.
/// </summary>
/// <inheritdoc />
[Ps1Xml(Label = "Subscription Id", Target = ViewControl.Table)]
public string Id { get; set; }

/// <summary>
/// The name of the subscription.
/// </summary>
/// <inheritdoc />
[Ps1Xml(Label = "Subscription Name", Target = ViewControl.Table)]
public string Name { get; set; }

/// <summary>
/// Gets or sets subscription State
/// </summary>
/// <inheritdoc />
[Ps1Xml]
Copy link
Member

Choose a reason for hiding this comment

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

I think we only want this one to go in the table view

public string State { get; set; }

/// <summary>
Expand All @@ -109,6 +107,7 @@ public PSAzureSubscription(PSObject other)
/// <summary>
/// The tenant home for the subscription.
/// </summary>
[Ps1Xml(Label = "Tenant Id", Target = ViewControl.Table)]
public string TenantId
{
get
Expand Down
6 changes: 6 additions & 0 deletions tools/RepoTasks/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositorypath" value="..\..\src\packages" />
</config>
</configuration>
36 changes: 36 additions & 0 deletions tools/RepoTasks/RepoTasks.Attributes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RepoTasks.Attributes")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP Inc.")]
[assembly: AssemblyProduct("RepoTasks.Attributes")]
[assembly: AssemblyCopyright("Copyright © HP Inc. 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8fa4a3ff-cb2c-48d9-9b44-a6e01c75fc2a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
123 changes: 123 additions & 0 deletions tools/RepoTasks/RepoTasks.Cmdlets.Tests/AppDomainWorkerShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace RepoTasks.Cmdlets.Tests
{
using CmdletsForTest;
using System;
using System.Collections.Generic;
using RemoteWorker;
using Xunit;
using System.IO;

public class AppDomainWorkerShould : IDisposable
{
private readonly AppDomain _domain;
private readonly IReflectionWorker _reflectionWorker;
private const string ExpectedAssemblyName = "RepoTasks.CmdletsForTest";

// Setup
public AppDomainWorkerShould()
{
var setup = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
};

_domain = AppDomain.CreateDomain("AppDomainIsolation: " + Guid.NewGuid(), null, setup);
var type = typeof(AppDomainWorker);
Assert.NotNull(type.FullName);

_reflectionWorker = (IReflectionWorker)_domain.CreateInstanceFromAndUnwrap(type.Assembly.Location, type.FullName);
}

// TearDown
public void Dispose()
{
if (_domain != null) AppDomain.Unload(_domain);
}

[Fact]
Copy link
Member

Choose a reason for hiding this comment

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

Use

[Trait(Category.AcceptanceType, Category.CheckIn)] for each of these tests, this will make them run in the check-in build. Also, please add this path and assembly to the test mappings file:https://github.com/Azure/azure-powershell/blob/preview/TestMappings.json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@markcowl
The project that owns the Category class with the constants (AcceptanceType, CheckIn) is dependent on many others. Is it a good idea just to copy paste the requires constants instead of adding all the dependencies?

[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ReturnCmdletTypesAndAssemblyNameWithCmdlets()
{
var cmdlets = new[] {"Test-Dummy"};
ReturnCmdletTypesAndAssemblyName(cmdlets);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ReturnCmdletTypesAndAssemblyNameWithoutCmdlets()
{
ReturnCmdletTypesAndAssemblyName(null);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ReturnCmdletTypesAndAssemblyNameWithEmptyCmdlets()
{
var cmdlets = new string[] {};
ReturnCmdletTypesAndAssemblyName(cmdlets);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void BuildConfiguration()
{
var cmdlets = new[] { "Test-Dummy" };
var types = ReturnCmdletTypesAndAssemblyName(cmdlets);
var conf = new AppDomainWorker().BuildXmlConfiguration(types);
Assert.Equal(4, conf.ViewDefinitions.Views.Length);

var view0 = conf.ViewDefinitions.Views[0];
Assert.Equal($"{ExpectedAssemblyName}.Models.PsDummyOutput1", view0?.ViewSelectedBy?.TypeName);

var expecterProps1 = new[] { "RequestId", "StatusCode", "Id", "Name", "Type" };
var columnItems = view0?.TableControl?.TableRowEntries[0]?.TableColumnItems;
Assert.NotNull(columnItems);
Assert.Equal(expecterProps1.Length, columnItems.Length);
foreach (var expectedProp in expecterProps1)
{
Assert.Contains(columnItems, ci => ci.PropertyName == expectedProp);
}
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void DoWorkRemotelyAndReturnFilname()
{
var assemblyFileName = Path.GetFileName(typeof(TestDummyCommand).Assembly.Location);
var assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyFileName);
var cmdlets = new[] { "Test-Dummy" };
var filename =_reflectionWorker.BuildFormatPs1Xml(assemblyPath, cmdlets, false);
Assert.NotNull(filename);
Assert.Equal($"{ExpectedAssemblyName}.generated.format.ps1xml", filename);
}

private static IList<Type> ReturnCmdletTypesAndAssemblyName(string[] cmdlets)
{
var assemblyPath = typeof(TestDummyCommand).Assembly.Location;
string assembyName;
var types = AppDomainWorker.GetCmdletTypes(assemblyPath, cmdlets, out assembyName);
Assert.Equal($"{ExpectedAssemblyName}", assembyName);
Assert.True(types.Count >= 1);
Assert.Contains(types, t => t.FullName == $"{ExpectedAssemblyName}.TestDummyCommand");
if (cmdlets != null && cmdlets?.Length == 1)
{
Assert.DoesNotContain(types, t => t.FullName == $"{ExpectedAssemblyName}.TestDummyTwoCommand");
}

return types;
}
}
}
8 changes: 8 additions & 0 deletions tools/RepoTasks/RepoTasks.Cmdlets.Tests/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace RepoTasks.Cmdlets.Tests
{
class Category
{
public const string AcceptanceType = "AcceptanceType";
public const string CheckIn = "CheckIn";
}
}
87 changes: 87 additions & 0 deletions tools/RepoTasks/RepoTasks.Cmdlets.Tests/Dummy.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@{

# Script module or binary module file associated with this manifest.
RootModule = ''

# Version number of this module.
ModuleVersion = '3.4.0'

# ID used to uniquely identify this module
GUID = '0a83c907-1ffb-4d87-a492-c65ac7d7ed37'

# Author of this module
Author = 'Microsoft Corporation'

# Company or vendor of this module
CompanyName = 'Microsoft Corporation'

# Copyright statement for this module
Copyright = 'Microsoft Corporation. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Test'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '3.0'

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module
DotNetFrameworkVersion = '4.5.2'

# Minimum version of the common language runtime (CLR) required by this module
CLRVersion = '4.0'

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
#TypesToProcess = 'Dummy.Types.ps1xml'

# Format files (.ps1xml) to be loaded when importing this module
#FormatsToProcess = ''

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('.\RepoTasks.CmdletsForTest.dll')

# Functions to export from this module
# FunctionsToExport = @()

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
# VariablesToExport = @()

# Aliases to export from this module
AliasesToExport = ''

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
Loading