Skip to content

Commit

Permalink
add-smoke-tests (#141)
Browse files Browse the repository at this point in the history
* Add missing comments

* Fix .NET unit test execution logic

* Remove obsolete files

* Change hardcoded throw for call to ErrorUtil

* Add unit tests

* Fix output file names

* Change PowerShell package ref to private

* Change PowerShell ref to full SDK, and drop net471

* Optimize reflection lookup logic

* Add PowerShell test fixture

* Remove platform-dependent tests

* Add unit tests

* Add missing [Fact] attribute

* Update gitignore

* Fix typos

* Add workspace settings file

* Fix missing tags in comment

* Fix flaky test

* Improve interaction with PS

* Improve parameter override logic

* Refactor Mru class

* Remove stale file

* Fix PS flaky behavior

* Add ThrowIfNull method

* Refactor simple methods into expression-bodied

* Add separate Merge and Override methods

* Update release notes
  • Loading branch information
igoravl authored Sep 8, 2021
1 parent 8edd4d2 commit 36af008
Show file tree
Hide file tree
Showing 37 changed files with 1,023 additions and 301 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,7 @@ TfsCmdlets.PSDesktop.xml
# JetBrains Rider project

CSharp/.idea/**

# Miscellaneous files

.testtemplate.cs
13 changes: 0 additions & 13 deletions CSharp/TfsCmdlets.Tests.Integration.PSCore/UnitTest1.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
14 changes: 0 additions & 14 deletions CSharp/TfsCmdlets.Tests.Integration.PSDesktop/UnitTest1.cs

This file was deleted.

40 changes: 40 additions & 0 deletions CSharp/TfsCmdlets.Tests.UnitTests/PowerShellFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Management.Automation;
using System;
using System.Management.Automation.Runspaces;

namespace TfsCmdlets.Tests.UnitTests
{
public class PowerShellFixture : IDisposable
{
private bool _IsDisposed;

public PowerShell PowerShell {get; private set;}

public PowerShellFixture()
{
PowerShell = PowerShell.Create();
Runspace.DefaultRunspace = PowerShell.Runspace;
}

protected virtual void Dispose(bool disposing)
{
if (!_IsDisposed)
{
if (disposing)
{
PowerShell.Dispose();
}

Runspace.DefaultRunspace = null;
PowerShell = null;
_IsDisposed = true;
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0" />
<ProjectReference Include="..\TfsCmdlets\TfsCmdlets.csproj" />
</ItemGroup>

</Project>
13 changes: 0 additions & 13 deletions CSharp/TfsCmdlets.Tests.UnitTests/UnitTest1.cs

This file was deleted.

24 changes: 24 additions & 0 deletions CSharp/TfsCmdlets.Tests.UnitTests/Util/EnvironmentUtil_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// using Xunit;
// using TfsCmdlets.Util;
// using System;

// namespace TfsCmdlets.Tests.UnitTests.Util
// {
// public class EnvironmentUtil_Tests
// {
// [Fact]
// [Trait("Platform", "Core")]
// public void PSEdition_Returns_Core_When_PSCore()
// {
// Assert.Equal("Core", EnvironmentUtil.PSEdition);
// }

// [Fact]
// [Trait("Platform", "Desktop")]
// public void PSEdition_Returns_Desktop_When_PSWindows()
// {
// Assert.Equal("Desktop", EnvironmentUtil.PSEdition);
// }

// }
// }
32 changes: 32 additions & 0 deletions CSharp/TfsCmdlets.Tests.UnitTests/Util/ErrorUtil_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Xunit;
using TfsCmdlets.Util;
using System;

namespace TfsCmdlets.Tests.UnitTests.Util
{
public class ErrorUtil_Tests
{
[Fact]
public void ThrowDesktopOnlyCmdlet_Throws_NotSupportedException()
{
Assert.Throws<NotSupportedException>(() =>
ErrorUtil.ThrowDesktopOnlyCmdlet()
);
}

[Fact]
public void ThrowIfNotFound_Throws_ArgumentException()
{
Assert.Throws<ArgumentException>("test", () =>
ErrorUtil.ThrowIfNotFound(null, "test", "searchCriteria")
);
}

[Fact]
public void ThrowIfNull_Throws_ArgumentNullException()
{
Assert.Throws<ArgumentNullException>("myVar", () =>
ErrorUtil.ThrowIfNull(null, "myVar"));
}
}
}
92 changes: 92 additions & 0 deletions CSharp/TfsCmdlets.Tests.UnitTests/Util/LazyProperty_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Xunit;
using TfsCmdlets.Util;
using TfsCmdlets.Extensions;
using System;
using System.Linq;
using System.Management.Automation;
using System.Collections;
using System.Management.Automation.Runspaces;

namespace TfsCmdlets.Tests.UnitTests.Util
{
public class LazyProperty_Tests: IClassFixture<PowerShellFixture>
{
private PowerShell _PowerShell;

public LazyProperty_Tests(PowerShellFixture fixture)
{
_PowerShell = fixture.PowerShell;
}

[Fact]
public void PropertyBag_Is_Created_On_First_Call()
{
const string propName = "__PropertyBag";

var returnValues = _PowerShell.AddScript(@"
Function PropertyBag_Is_Created_On_First_Call
{
$pso = [PSCustomObject] @{Foo='Bar'}
$pso | Add-Member -Name MyLazy -MemberType ScriptProperty `
-Value { [TfsCmdlets.Util.LazyProperty]::Get($pso, 'MyLazy', {[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Next()}) }
[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Reset()
$val1 = $pso.MyLazy
return @($val1, $pso)
}
$r = PropertyBag_Is_Created_On_First_Call; echo $r"
).Invoke().ToList();

Assert.Equal(1, returnValues[0]);
Assert.True(((PSObject) returnValues[1]).Properties.Match(propName, PSMemberTypes.NoteProperty).Any(), $"Property '{propName}' not found");
}

[Fact]
public void Lazy_Property_Is_Called_Only_Once()
{
var returnValues = _PowerShell.AddScript(@"
Function Lazy_Property_Is_Called_Only_Once
{
$pso = [PSCustomObject]@{Foo='Bar'}
$pso | Add-Member -Name MyLazy -MemberType ScriptProperty `
-Value { [TfsCmdlets.Util.LazyProperty]::Get($pso, 'MyLazy', {[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Next()}) }
[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Reset()
$val1 = $pso.MyLazy
$val2 = $pso.MyLazy
return $val1, $val2
}
$r = Lazy_Property_Is_Called_Only_Once; echo $r"
).Invoke().ToList();

Assert.Equal(1, returnValues[0].BaseObject);
Assert.Equal(1, returnValues[1].BaseObject);
}
}

public static class LazyPropertySimulator
{
private static int _Counter;

static LazyPropertySimulator()
{
Reset();
}

public static int Next() {
return _Counter++;
}

public static void Reset()
{
_Counter = 1;
}
}
}
115 changes: 115 additions & 0 deletions CSharp/TfsCmdlets.Tests.UnitTests/Util/Mru_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using TfsCmdlets.Util;
using Xunit;

namespace TfsCmdlets.Tests.UnitTests
{
public class Mru_Tests
{
private readonly string[] _SampleItems = new[] { "item1", "item2", "item3" };

[Fact]
public void Supports_Add_All()
{
var mruList = new MruList<string>();

mruList.Append(_SampleItems);

Assert.Collection(mruList,
i => Assert.Equal("item1", i),
i => Assert.Equal("item2", i),
i => Assert.Equal("item3", i)
);
}

[Fact]
public void Supports_Initial_Items()
{
var mruList = new MruList<string>(_SampleItems);

Assert.Collection(mruList,
i => Assert.Equal("item1", i),
i => Assert.Equal("item2", i),
i => Assert.Equal("item3", i)
);
}

[Fact]
public void Can_Find_Items()
{
var mruList = new MruList<string>(_SampleItems);

Assert.Equal("item3", mruList.Get(i => i.EndsWith("3")));
}

[Fact]
public void Get_Returns_Top_Item()
{
var mruList = new MruList<string>(_SampleItems);

Assert.Equal("item1", mruList.Get());
}

[Fact]
public void Get_Find_Moves_Item_To_Top()
{
var mruList = new MruList<string>(_SampleItems);

var item = mruList.Get(i => i.EndsWith("3"));

Assert.Equal(item, mruList.Get());
}

[Fact]
public void Can_Limit_Initial_Capacity()
{
var mruList = new MruList<string>(_SampleItems, 2);

Assert.Collection(mruList,
i => Assert.Equal("item1", i),
i => Assert.Equal("item2", i)
);
}

[Fact]
public void Supports_Collection_Initialization()
{
var mruList = new MruList<string>()
{
"item1",
"item2",
"item3"
};

Assert.Collection(mruList,
i => Assert.Equal("item1", i),
i => Assert.Equal("item2", i),
i => Assert.Equal("item3", i)
);
}

[Fact]
public void Can_Discard_Lru_On_Capacity_Reached()
{
var mruList = new MruList<string>(_SampleItems, 3);

mruList.Insert("item4");

Assert.Equal(3, mruList.Count);

Assert.Collection(mruList,
i => Assert.Equal("item4", i),
i => Assert.Equal("item1", i),
i => Assert.Equal("item2", i)
);
}

[Fact]
public void Throw_On_Append_When_Capacity_Reached()
{
var mruList = new MruList<string>(_SampleItems, 3);

Assert.Throws<Exception>(() => mruList.Append("item4"));
}
}
}
Loading

0 comments on commit 36af008

Please sign in to comment.