Skip to content

Commit

Permalink
Enabling FxCop on tests (#6481)
Browse files Browse the repository at this point in the history
  • Loading branch information
crutkas committed Sep 9, 2020
1 parent fb1888f commit 1027b7d
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 118 deletions.
24 changes: 15 additions & 9 deletions src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public static string GetSerializedCustomAction(string moduleName, string actionN
ModuleAction = customAction,
};

var sendCustomAction = new SendCustomAction(moduleName);
sendCustomAction.Action = moduleCustomAction;
var sendCustomAction = new SendCustomAction(moduleName)
{
Action = moduleCustomAction,
};

return sendCustomAction.ToJsonString();
}

Expand All @@ -53,12 +56,15 @@ public static FileSystemWatcher GetFileWatcher(string moduleName, string fileNam
Directory.CreateDirectory(path);
}

var watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.Filter = fileName;
watcher.NotifyFilter = NotifyFilters.LastWrite;
var watcher = new FileSystemWatcher
{
Path = path,
Filter = fileName,
NotifyFilter = NotifyFilters.LastWrite,
EnableRaisingEvents = true,
};

watcher.Changed += (o, e) => onChangedCallback();
watcher.EnableRaisingEvents = true;

return watcher;
}
Expand All @@ -71,11 +77,11 @@ private static string LocalApplicationDataFolder()
[DllImport("user32.dll")]
private static extern bool AllowSetForegroundWindow(int dwProcessId);

private static interop.LayoutMapManaged layoutMap = new interop.LayoutMapManaged();
private static readonly interop.LayoutMapManaged LayoutMap = new interop.LayoutMapManaged();

public static string GetKeyName(uint key)
{
return layoutMap.GetKeyName(key);
return LayoutMap.GetKeyName(key);
}

public static string GetProductVersion()
Expand Down
3 changes: 1 addition & 2 deletions src/modules/colorPicker/ColorPickerUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public static void Main(string[] args)
protected override void OnStartup(StartupEventArgs e)
{
// allow only one instance of color picker
bool createdNew;
_instanceMutex = new Mutex(true, @"Global\ColorPicker", out createdNew);
_instanceMutex = new Mutex(true, @"Global\ColorPicker", out bool createdNew);
if (!createdNew)
{
_instanceMutex = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class UserSettings : IUserSettings
private const string ColorPickerModuleName = "ColorPicker";
private const string DefaultActivationShortcut = "Ctrl + Break";
private const int MaxNumberOfRetry = 5;
private FileSystemWatcher _watcher;

private object _loadingSettingsLock = new object();
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0052:Remove unread private members", Justification = "Actually, call back is LoadSettingsFromJson")]
private readonly FileSystemWatcher _watcher;

private readonly object _loadingSettingsLock = new object();

[ImportingConstructor]
public UserSettings()
Expand Down
5 changes: 5 additions & 0 deletions src/modules/imageresizer/tests/ImageResizerUITest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers">
<Version>3.3.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq">
<Version>4.14.5</Version>
</PackageReference>
Expand Down
9 changes: 5 additions & 4 deletions src/modules/imageresizer/tests/Models/CustomSizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ namespace ImageResizer.Models
public class CustomSizeTests
{
[Fact]
public void Name_works()
public void NameWorks()
{
var size = new CustomSize();

size.Name = "Ignored";
var size = new CustomSize
{
Name = "Ignored",
};

Assert.Equal(Resources.Input_Custom, size.Name);
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/imageresizer/tests/Models/ResizeBatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ResizeBatchTests
private static readonly string EOL = Environment.NewLine;

[Fact]
public void FromCommandLine_works()
public void FromCommandLineWorks()
{
var standardInput =
"Image1.jpg" + EOL +
Expand Down Expand Up @@ -56,7 +56,7 @@ public void Process_executes_in_parallel()
}*/

[Fact]
public void Process_aggregates_errors()
public void ProcessAggregatesErrors()
{
var batch = CreateBatch(file => throw new Exception("Error: " + file));
batch.Files.Add("Image1.jpg");
Expand All @@ -81,7 +81,7 @@ public void Process_aggregates_errors()
}

[Fact]
public void Process_reports_progress()
public void ProcessReportsProgress()
{
var batch = CreateBatch(_ => { });
batch.Files.Add("Image1.jpg");
Expand Down
78 changes: 49 additions & 29 deletions src/modules/imageresizer/tests/Models/ResizeOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ namespace ImageResizer.Models
public class ResizeOperationTests : IDisposable
{
private readonly TestDirectory _directory = new TestDirectory();
private bool disposedValue;

[Fact]
public void Execute_copies_frame_metadata()
public void ExecuteCopiesFrameMetadata()
{
var operation = new ResizeOperation("Test.jpg", _directory, Settings());

Expand All @@ -30,7 +31,7 @@ public void Execute_copies_frame_metadata()
}

[Fact]
public void Execute_keeps_date_modified()
public void ExecuteKeepsDateModified()
{
var operation = new ResizeOperation("Test.png", _directory, Settings(s => s.KeepDateModified = true));

Expand All @@ -40,7 +41,7 @@ public void Execute_keeps_date_modified()
}

[Fact]
public void Execute_keeps_date_modified_when_replacing_originals()
public void ExecuteKeepsDateModifiedWhenReplacingOriginals()
{
var path = Path.Combine(_directory, "Test.png");
File.Copy("Test.png", path);
Expand All @@ -63,7 +64,7 @@ public void Execute_keeps_date_modified_when_replacing_originals()
}

[Fact]
public void Execute_replaces_originals()
public void ExecuteReplacesOriginals()
{
var path = Path.Combine(_directory, "Test.png");
File.Copy("Test.png", path);
Expand All @@ -76,7 +77,7 @@ public void Execute_replaces_originals()
}

[Fact]
public void Execute_transforms_each_frame()
public void ExecuteTransformsEachFrame()
{
var operation = new ResizeOperation("Test.gif", _directory, Settings());

Expand All @@ -92,7 +93,7 @@ public void Execute_transforms_each_frame()
}

[Fact]
public void Execute_uses_fallback_encoder()
public void ExecuteUsesFallbackEncoder()
{
var operation = new ResizeOperation(
"Test.ico",
Expand All @@ -105,7 +106,7 @@ public void Execute_uses_fallback_encoder()
}

[Fact]
public void Transform_ignores_orientation_when_landscape_to_portrait()
public void TransformIgnoresOrientationWhenLandscapeToPortrait()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -130,7 +131,7 @@ public void Transform_ignores_orientation_when_landscape_to_portrait()
}

[Fact]
public void Transform_ignores_orientation_when_portrait_to_landscape()
public void TransformIgnoresOrientationWhenPortraitToLandscape()
{
var operation = new ResizeOperation(
"TestPortrait.png",
Expand All @@ -155,7 +156,7 @@ public void Transform_ignores_orientation_when_portrait_to_landscape()
}

[Fact]
public void Transform_ignores_ignore_orientation_when_auto()
public void TransformIgnoresIgnoreOrientationWhenAuto()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -180,7 +181,7 @@ public void Transform_ignores_ignore_orientation_when_auto()
}

[Fact]
public void Transform_ignores_ignore_orientation_when_percent()
public void TransformIgnoresIgnoreOrientationWhenPercent()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -207,7 +208,7 @@ public void Transform_ignores_ignore_orientation_when_percent()
}

[Fact]
public void Transform_honors_shrink_only()
public void TransformHonorsShrinkOnly()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -232,7 +233,7 @@ public void Transform_honors_shrink_only()
}

[Fact]
public void Transform_ignores_shrink_only_when_percent()
public void TransformIgnoresShrinkOnlyWhenPercent()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -257,7 +258,7 @@ public void Transform_ignores_shrink_only_when_percent()
}

[Fact]
public void Transform_honors_shrink_only_when_auto_height()
public void TransformHonorsShrinkOnlyWhenAutoHeight()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -278,7 +279,7 @@ public void Transform_honors_shrink_only_when_auto_height()
}

[Fact]
public void Transform_honors_shrink_only_when_auto_width()
public void TransformHonorsShrinkOnlyWhenAutoWidth()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -299,7 +300,7 @@ public void Transform_honors_shrink_only_when_auto_width()
}

[Fact]
public void Transform_honors_unit()
public void TransformHonorsUnit()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -318,7 +319,7 @@ public void Transform_honors_unit()
}

[Fact]
public void Transform_honors_fit_when_Fit()
public void TransformHonorsFitWhenFit()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -337,7 +338,7 @@ public void Transform_honors_fit_when_Fit()
}

[Fact]
public void Transform_honors_fit_when_Fill()
public void TransformHonorsFitWhenFill()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -357,7 +358,7 @@ public void Transform_honors_fit_when_Fill()
}

[Fact]
public void Transform_honors_fit_when_Stretch()
public void TransformHonorsFitWhenStretch()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -377,9 +378,9 @@ public void Transform_honors_fit_when_Stretch()
}

[Fact]
public void GetDestinationPath_uniquifies_output_filename()
public void GetDestinationPathUniquifiesOutputFilename()
{
File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), new byte[0]);
File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), Array.Empty<byte>());

var operation = new ResizeOperation("Test.png", _directory, Settings());

Expand All @@ -389,10 +390,10 @@ public void GetDestinationPath_uniquifies_output_filename()
}

[Fact]
public void GetDestinationPath_uniquifies_output_filename_again()
public void GetDestinationPathUniquifiesOutputFilenameAgain()
{
File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), new byte[0]);
File.WriteAllBytes(Path.Combine(_directory, "Test (Test) (1).png"), new byte[0]);
File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), Array.Empty<byte>());
File.WriteAllBytes(Path.Combine(_directory, "Test (Test) (1).png"), Array.Empty<byte>());

var operation = new ResizeOperation("Test.png", _directory, Settings());

Expand All @@ -402,7 +403,7 @@ public void GetDestinationPath_uniquifies_output_filename_again()
}

[Fact]
public void GetDestinationPath_uses_fileName_format()
public void GetDestinationPathUsesFileNameFormat()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -415,7 +416,7 @@ public void GetDestinationPath_uses_fileName_format()
}

[Fact]
public void Execute_handles_directories_in_fileName_format()
public void ExecuteHandlesDirectoriesInFileNameFormat()
{
var operation = new ResizeOperation(
"Test.png",
Expand All @@ -427,10 +428,7 @@ public void Execute_handles_directories_in_fileName_format()
Assert.True(File.Exists(_directory + @"\Directory\Test (Test).png"));
}

public void Dispose()
=> _directory.Dispose();

private Settings Settings(Action<Settings> action = null)
private static Settings Settings(Action<Settings> action = null)
{
var settings = new Settings
{
Expand All @@ -449,5 +447,27 @@ private Settings Settings(Action<Settings> action = null)

return settings;
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_directory.Dispose();
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

0 comments on commit 1027b7d

Please sign in to comment.