public static TempFile CreateCSharpAnalyzerAssemblyWithTestAnalyzer(TempDirectory dir, string assemblyName) { var analyzerSource = @" using System; using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class TestAnalyzer : DiagnosticAnalyzer { public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { throw new NotImplementedException(); } } public override void Initialize(AnalysisContext context) { throw new NotImplementedException(); } }"; var metadata = dir.CopyFile(typeof(System.Reflection.Metadata.MetadataReader).Assembly.Location); var immutable = dir.CopyFile(typeof(ImmutableArray).Assembly.Location); var analyzer = dir.CopyFile(typeof(DiagnosticAnalyzer).Assembly.Location); var filesystem = dir.CreateFile("System.IO.FileSystem.dll").WriteAllBytes( TestResources.NetFX.v4_6_1038_0.Facades.System_IO_FileSystem); var analyzerCompilation = CSharpCompilation.Create( assemblyName, new SyntaxTree[] { SyntaxFactory.ParseSyntaxTree(analyzerSource) }, new MetadataReference[] { TestBase.SystemRuntimeNetstandard13FacadeRef.Value, MetadataReference.CreateFromFile(immutable.Path), MetadataReference.CreateFromFile(analyzer.Path) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); return(dir.CreateFile(assemblyName + ".dll").WriteAllBytes(analyzerCompilation.EmitToArray())); }
public void DirectoryWithSlashShouldNotBeRootOfRelativePath() { using (var t = new TempDirectory()) { t.CreateFile("a"); t.CreateFile("b"); var scan = FilesystemScanner.Scan(t.FullPath + Path.DirectorySeparatorChar).ToArray(); Assert.AreEqual("a", scan[0].RelativePath); Assert.AreEqual("b", scan[1].RelativePath); } }
public DotNetSdkTestBase() { Assert.True(s_dotnetInstallDir != null, $"SDK not found. Use {nameof(ConditionalFactAttribute)}(typeof({nameof(DotNetSdkAvailable)})) to skip the test if the SDK is not found."); DotNetPath = Path.Combine(s_dotnetInstallDir, s_dotnetExeName); var testBinDirectory = Path.GetDirectoryName(typeof(DotNetSdkTests).Assembly.Location); var sdksDir = Path.Combine(s_dotnetSdkPath, "Sdks"); ProjectName = "test"; ProjectFileName = ProjectName + ".csproj"; Configuration = "Debug"; TargetFramework = "netstandard2.0"; ProjectDir = Temp.CreateDirectory(); ObjDir = ProjectDir.CreateDirectory("obj"); OutDir = ProjectDir.CreateDirectory("bin").CreateDirectory(Configuration).CreateDirectory(TargetFramework); Project = ProjectDir.CreateFile(ProjectFileName).WriteAllText(s_projectSource); ProjectDir.CreateFile("TestClass.cs").WriteAllText(s_classSource); // avoid accidental dependency on files outside of the project directory: ProjectDir.CreateFile("Directory.Build.props").WriteAllText("<Project/>"); ProjectDir.CreateFile("Directory.Build.targets").WriteAllText("<Project/>"); ProjectDir.CreateFile(".editorconfig").WriteAllText("root = true"); var csharpCoreTargets = Path.Combine(testBinDirectory, "Microsoft.CSharp.Core.targets"); var visualBasicCoreTargets = Path.Combine(testBinDirectory, "Microsoft.VisualBasic.Core.targets"); Assert.True(File.Exists(csharpCoreTargets)); Assert.True(File.Exists(visualBasicCoreTargets)); EnvironmentVariables = new Dictionary <string, string>() { { "CSharpCoreTargetsPath", csharpCoreTargets }, { "VisualBasicCoreTargetsPath", visualBasicCoreTargets }, { "MSBuildSDKsPath", sdksDir }, { "DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR", sdksDir } }; var restoreResult = ProcessUtilities.Run(DotNetPath, $@"msbuild ""{Project.Path}"" /t:restore /bl:{Path.Combine(ProjectDir.Path, "restore.binlog")}", additionalEnvironmentVars: EnvironmentVariables); Assert.True(restoreResult.ExitCode == 0, $"Failed with exit code {restoreResult.ExitCode}: {restoreResult.Output}"); Assert.True(File.Exists(Path.Combine(ObjDir.Path, "project.assets.json"))); Assert.True(File.Exists(Path.Combine(ObjDir.Path, ProjectFileName + ".nuget.g.props"))); Assert.True(File.Exists(Path.Combine(ObjDir.Path, ProjectFileName + ".nuget.g.targets"))); }
public void OrderShouldBeFilesThenDirectores() { using (var t = new TempDirectory()) { t.CreateDir("b"); t.CreateDir("a"); t.CreateFile("b\\b.txt"); t.CreateFile("b\\a.txt"); t.CreateFile("a\\b.txt"); t.CreateFile("a\\a.txt"); t.CreateFile("z.txt"); t.CreateFile("y.txt"); t.CreateFile("x.txt"); var scan = FilesystemScanner.Scan(t.FullPath + Path.DirectorySeparatorChar).ToArray(); Assert.AreEqual("x.txt", scan[0].RelativePath); // files first Assert.AreEqual("y.txt", scan[1].RelativePath); Assert.AreEqual("z.txt", scan[2].RelativePath); Assert.AreEqual("a", scan[3].RelativePath); // then all of directory 'a' Assert.AreEqual("a\\a.txt", scan[4].RelativePath); Assert.AreEqual("a\\b.txt", scan[5].RelativePath); Assert.AreEqual("b", scan[6].RelativePath); // then all of directory 'b' Assert.AreEqual("b\\a.txt", scan[7].RelativePath); Assert.AreEqual("b\\b.txt", scan[8].RelativePath); } }
public void SingleFileShouldHaveNoRelativeParents() { using (var t = new TempDirectory()) { t.CreateFile("a"); var scan = FilesystemScanner.Scan($"{t.FullPath}\\a").ToArray(); Assert.AreEqual("a", scan[0].Info.Name); } }
public void DirectoryShouldBeRootOfRelativePath() { using (var t = new TempDirectory()) { t.CreateFile("a"); var scan = FilesystemScanner.Scan(t.FullPath).ToArray(); Assert.AreEqual(t.Name, scan[0].RelativePath); Assert.AreEqual($"{t.Name}\\a", scan[1].RelativePath); } }
private static CompiledFile CompileLibrary(TempDirectory dir, string fileName, string assemblyName, string source, params MetadataReference[] references) { var file = dir.CreateFile(fileName); var compilation = CreateCompilation( new[] { source }, assemblyName: assemblyName, references: references.Concat(new[] { MetadataReference.CreateFromAssemblyInternal(typeof(object).Assembly) }), options: fileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) ? TestOptions.ReleaseExe : TestOptions.ReleaseDll); var image = compilation.EmitToArray(); file.WriteAllBytes(image); return new CompiledFile { Path = file.Path, Image = image }; }
public async Task B2UploadFile() { credentials = Credentials.Read(); var b2 = new B2(); await b2.Login(credentials.accountId, credentials.applicationKey, bucketName).ConfigureAwait(false); using (var t = new TempDirectory()) { t.CreateFile("test.txt"); var sourcePath = Path.Combine(t.FullPath, "test.txt"); var destinationPath = "a/b/c/test.txt"; await b2.UploadFile(sourcePath, destinationPath); } }
public static (string Path, ImmutableArray<byte> Image) CompileLibrary( TempDirectory dir, string fileName, string assemblyName, string source, params MetadataReference[] references) { var file = dir.CreateFile(fileName); var compilation = CreateEmptyCompilation( new[] { source }, assemblyName: assemblyName, references: TargetFrameworkUtil.GetReferences(TargetFramework.NetStandard20, references), options: fileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) ? TestOptions.ReleaseExe : TestOptions.ReleaseDll); var image = compilation.EmitToArray(); file.WriteAllBytes(image); return (file.Path, image); }
private ProcessResult RunCommandLineCompiler( string compilerPath, string arguments, TempDirectory currentDirectory, IEnumerable <KeyValuePair <string, string> > filesInDirectory, IEnumerable <KeyValuePair <string, string> > additionalEnvironmentVars = null) { foreach (var pair in filesInDirectory) { TempFile file = currentDirectory.CreateFile(pair.Key); file.WriteAllText(pair.Value); } return(RunCommandLineCompiler( compilerPath, arguments, currentDirectory.Path, additionalEnvironmentVars: AddForLoggingEnvironmentVars(additionalEnvironmentVars))); }
internal CompiledFile CompileLibrary(TempDirectory dir, string fileName, string assemblyName, string source, params MetadataReference[] references) { const string Prefix = "RoslynTestFile_"; fileName = Prefix + fileName; assemblyName = Prefix + assemblyName; var file = dir.CreateFile(fileName); var compilation = CreateCompilation( new[] { source }, assemblyName: assemblyName, references: references.Concat(new[] { MetadataReference.CreateFromAssembly(typeof(object).Assembly) }), options: fileName.EndsWith(".exe") ? TestOptions.ReleaseExe : TestOptions.ReleaseDll); var image = compilation.EmitToArray(); file.WriteAllBytes(image); return(new CompiledFile { Path = file.Path, Image = image }); }