public void InvalidDll_Throws() { var path = BaseDir.Combine("test.txt").WriteAllText("this is definitely not a valid dll"); File.Exists(path).ShouldBeTrue(); Should.Throw <PeVerifyException>(() => PeVerify.Verify(path)); }
public void BasicTest() { var assemblyPath = Assembly.GetExecutingAssembly().Location.ToNPath().Parent; var systemUnderTestPath = assemblyPath.Combine("Minimal.Systems.dll"); var testPath = assemblyPath.Combine("Minimal.Tests.dll"); // TODO: use testAssembly to scan for uses of Substitute.For and retrieve System.DateTime.Now and Class.Add using (var systemUnderTest = AssemblyDefinition.ReadAssembly(systemUnderTestPath)) using (var mscorlib = AssemblyDefinition.ReadAssembly(typeof(object).Assembly.Location)) { // list of all stuff we detect an NSub extension called on it var mockedMethodDefinitions = new[] { mscorlib.MainModule.GetType("System.DateTime").Properties.Single(p => p.Name == "Now").GetMethod, systemUnderTest.MainModule.GetType("Inner").Methods.Single(m => m.Name == "Add"), }; Patch(mockedMethodDefinitions); Write(systemUnderTest, systemUnderTestPath); Write(mscorlib, BaseDir.Combine("mscorlib.dll")); } // would like to do this in an appdomain but complicated using (ElevatedSubstitutionContext.AutoHook()) { var testAssembly = Assembly.LoadFile(testPath); var fixtureType = testAssembly.GetType("Fixture", true); var fixture = Activator.CreateInstance(fixtureType); var mockMethod = fixtureType.GetMethod("Mock"); mockMethod.Invoke(fixture, Array.Empty <object>()); } }
protected NPath Compile(string testAssemblyName, string sourceCode, params string[] dependentAssemblyNames) { // prefix the assembly name because they are globally unique and don't want to ever collide var testAssemblyPath = BaseDir .Combine($"{TestContext.CurrentContext.GetFixtureName()}_{testAssemblyName}") .ChangeExtension(".dll"); // set up to compile var compiler = new Microsoft.CSharp.CSharpCodeProvider(); var compilerArgs = new CompilerParameters { OutputAssembly = testAssemblyPath, IncludeDebugInformation = true, CompilerOptions = "/o- /debug+ /warn:0" }; compilerArgs.ReferencedAssemblies.Add(typeof(int).Assembly.Location); // mscorlib // TODO: use typecache var assemblies = AppDomain.CurrentDomain .GetAssemblies() .Where(a => !a.IsDynamic) .ToDictionary(a => a.GetName().Name, a => a.Location.ToNPath(), StringComparer.OrdinalIgnoreCase); foreach (var dependentAssemblyName in dependentAssemblyNames) { // we may have already copied it in var path = BaseDir.Combine(dependentAssemblyName).ChangeExtension(".dll"); // if not, if (!path.Exists() && assemblies.TryGetValue(dependentAssemblyName, out path)) { path.Copy(BaseDir.Combine(path.FileName)); } compilerArgs.ReferencedAssemblies.Add(path); } // compile and handle errors var compilerResult = compiler.CompileAssemblyFromSource(compilerArgs, sourceCode); if (compilerResult.Errors.Count > 0) { var errorText = compilerResult.Errors .OfType <CompilerError>() .Select(e => $"({e.Line},{e.Column}): error {e.ErrorNumber}: {e.ErrorText}") .Prepend("Compiler errors:") .StringJoin("\n"); throw new Exception(errorText); } testAssemblyPath.ShouldBe(new NPath(compilerResult.PathToAssembly)); PeVerify.Verify(testAssemblyPath); // sanity check on what the compiler generated return(testAssemblyPath); }
public void ForceDeleteFile_WithMissingFile_DoesNotThrow() { var path = BaseDir.Combine("missing.txt"); path.FileExists().ShouldBeFalse(); Should.NotThrow(() => SafeFile.ForceDeleteFile(path)); path.FileExists().ShouldBeFalse(); }
public void ReadAllFloats_WithFloatsInFile_ReturnsFloats() { BaseDir.Combine("1.txt").WriteAllText("0").ReadAllFloats().ShouldBe(new[] { 0f }); BaseDir.Combine("2.txt").WriteAllText("-123.456").ReadAllFloats().ShouldBe(new[] { -123.456f }); BaseDir.Combine("3.txt").WriteAllText("\n 456.789 \n").ReadAllFloats().ShouldBe(new[] { 456.789f }); BaseDir.Combine("4.txt").WriteAllText("\n 1.2 \r\n 2.3").ReadAllFloats().ShouldBe(new[] { 1.2f, 2.3f }); BaseDir.Combine("5.txt").WriteAllText("\n 1.3 2.4").ReadAllFloats().ShouldBe(new[] { 1.3f, 2.4f }); BaseDir.Combine("6.txt").WriteAllText("\n 1.4 2.5\n ").ReadAllFloats().ShouldBe(new[] { 1.4f, 2.5f }); }
public void ReadAllInts_WithIntsInFile_ReturnsInts() { BaseDir.Combine("1.txt").WriteAllText("0").ReadAllInts().ShouldBe(new[] { 0 }); BaseDir.Combine("2.txt").WriteAllText("-123").ReadAllInts().ShouldBe(new[] { -123 }); BaseDir.Combine("3.txt").WriteAllText("\n 456 \n").ReadAllInts().ShouldBe(new[] { 456 }); BaseDir.Combine("4.txt").WriteAllText("\n 1 \r\n 2").ReadAllInts().ShouldBe(new[] { 1, 2 }); BaseDir.Combine("5.txt").WriteAllText("\n 1 2").ReadAllInts().ShouldBe(new[] { 1, 2 }); BaseDir.Combine("6.txt").WriteAllText("\n 1 2\n ").ReadAllInts().ShouldBe(new[] { 1, 2 }); }
public void Format_WithoutPackageRootSpecified_DoesNotThrow() { var logger = new StringLogger(); App.Execute(new[] { "format", BaseDir.Combine("file").WriteAllText("") }, logger); logger.ErrorsAsString.ShouldBeEmpty(); logger.InfosAsString.ShouldBeEmpty(); }
public void Format_WithNoEditorConfig_DoesNothing() { var path = BaseDir.Combine("file.txt").WriteAllText(k_UnformattedSource); var result = Execute("format", path); result.ErrorsAsString.ShouldBeEmpty(); result.InfosAsString.ShouldBeEmpty(); path.ReadAllText().ShouldBe(k_UnformattedSource); }
public void Format_WithEditorConfig_FormatsFile() { WriteRootEditorConfig("[*]", "formatters=uncrustify,generic"); var path = BaseDir.Combine("file.cs").WriteAllText(k_UnformattedSource); var result = Execute("format", path); result.ErrorsAsString.ShouldBeEmpty(); result.InfosAsString.ShouldBeEmpty(); path.ReadAllText().ShouldBe(k_FormattedSource); }
public void AtomicWrite_WithExistingReadOnlyTempAndBakFiles_OverwritesFilesAndOperatesNormally() { var path = BaseDir.Combine("test.txt").WriteAllText("test"); var temp = (path + SafeFile.TmpExtension).ToNPath().CreateFile(); var backup = (path + SafeFile.BakExtension).ToNPath().CreateFile(); SafeFile.SetReadOnly(temp); SafeFile.SetReadOnly(backup); SafeFile.AtomicWrite(path, tmpPath => tmpPath.ToNPath().WriteAllText("new")); temp.FileExists().ShouldBeFalse(); backup.FileExists().ShouldBeFalse(); path.ReadAllText().ShouldBe("new"); }
public void Format_WithBadPackagePath_ReturnsError() { // minimum required to trigger need for coding package guts WriteRootEditorConfig("[*]", "formatters=uncrustify"); var logger = new StringLogger(); App.Execute(new[] { "format", "--package-root", "does_not_exist", BaseDir.Combine("file").WriteAllText("") }, logger); logger.ErrorsAsString.ShouldContain("Invalid package root"); }
public void Format_WithMultipleFilesIncludingOneBadPath_FormatsValidAndErrorsForBad() { WriteRootEditorConfig("[*]", "formatters=uncrustify,generic"); var paths = new[] { BaseDir.Combine("file.cs").WriteAllText(k_UnformattedSource), BaseDir.Combine("file2.cs"), BaseDir.Combine("file3.cs").WriteAllText(k_UnformattedSource), }; var result = Execute("format", paths[0], paths[1], paths[2]); result.Errors.Count.ShouldBe(1); result.ErrorsAsString.ShouldMatch(@"Could not find file.*file2\.cs"); result.InfosAsString.ShouldBeEmpty(); paths[0].ReadAllText().ShouldBe(k_FormattedSource); paths[2].ReadAllText().ShouldBe(k_FormattedSource); }
public NPath Compile(string testAssemblyName, string sourceCode, params string[] dependentAssemblyNames) { var testAssemblyPath = BaseDir.Combine(testAssemblyName + ".dll"); // set up to compile var compiler = new Microsoft.CSharp.CSharpCodeProvider(); var compilerArgs = new CompilerParameters { OutputAssembly = testAssemblyPath, IncludeDebugInformation = true, CompilerOptions = "/o- /debug+ /warn:0" }; compilerArgs.ReferencedAssemblies.Add(typeof(int).Assembly.Location); // mscorlib compilerArgs.ReferencedAssemblies.AddRange( dependentAssemblyNames.Select(n => BaseDir.Combine(n + ".dll").ToString())); // compile and handle errors var compilerResult = compiler.CompileAssemblyFromSource(compilerArgs, sourceCode); if (compilerResult.Errors.Count > 0) { var errorText = compilerResult.Errors .OfType <CompilerError>() .Select(e => $"({e.Line},{e.Column}): error {e.ErrorNumber}: {e.ErrorText}") .Prepend("Compiler errors:") .StringJoin("\n"); throw new Exception(errorText); } testAssemblyPath.ShouldBe(new NPath(compilerResult.PathToAssembly)); PeVerify.Verify(testAssemblyPath); // sanity check on what the compiler generated return(testAssemblyPath); }
public void ReadAllFloats_WithNoFloatsInFile_ReturnsEmpty() { BaseDir.Combine("1.txt").WriteAllText("").ReadAllFloats().ShouldBeEmpty(); BaseDir.Combine("2.txt").WriteAllText("\n \n").ReadAllFloats().ShouldBeEmpty(); }