Esempio n. 1
0
        public void RuleSet_GeneralOption_CPS()
        {
            string ruleSetSource = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RuleSet Name=""Ruleset1"" Description=""Test""  ToolsVersion=""12.0"">
  <IncludeAll Action=""Error"" />
</RuleSet>
";

            using (var ruleSetFile = new DisposableFile())
                using (var environment = new TestEnvironment())
                    using (var project = CSharpHelpers.CreateCSharpCPSProject(environment, "Test"))
                    {
                        File.WriteAllText(ruleSetFile.Path, ruleSetSource);

                        var workspaceProject = environment.Workspace.CurrentSolution.Projects.Single();
                        var options          = (CSharpCompilationOptions)workspaceProject.CompilationOptions;

                        Assert.Equal(expected: ReportDiagnostic.Default, actual: options.GeneralDiagnosticOption);

                        project.SetOptions($"/ruleset:{ruleSetFile.Path}");

                        workspaceProject = environment.Workspace.CurrentSolution.Projects.Single();
                        options          = (CSharpCompilationOptions)workspaceProject.CompilationOptions;

                        Assert.Equal(expected: ReportDiagnostic.Error, actual: options.GeneralDiagnosticOption);
                    }
        }
Esempio n. 2
0
        public void RuleSet_SpecificOptions_CPS()
        {
            string ruleSetSource = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RuleSet Name=""Ruleset1"" Description=""Test""  ToolsVersion=""12.0"">
  <IncludeAll Action=""Warning"" />
  <Rules AnalyzerId=""Microsoft.Analyzers.ManagedCodeAnalysis"" RuleNamespace=""Microsoft.Rules.Managed"">
    <Rule Id=""CA1012"" Action=""Error"" />
  </Rules>
</RuleSet>
";

            using (var ruleSetFile = new DisposableFile())
                using (var environment = new TestEnvironment())
                    using (var project = CSharpHelpers.CreateCSharpCPSProject(environment, "Test"))
                    {
                        // Verify SetRuleSetFile updates the ruleset.
                        File.WriteAllText(ruleSetFile.Path, ruleSetSource);
                        project.SetOptions($"/ruleset:{ruleSetFile.Path}");

                        // We need to explicitly update the command line arguments so the new ruleset is used to update options.
                        project.SetOptions($"/ruleset:{ruleSetFile.Path}");
                        var ca1012DiagnosticOption = environment.Workspace.CurrentSolution.Projects.Single().CompilationOptions.SpecificDiagnosticOptions["CA1012"];
                        Assert.Equal(expected: ReportDiagnostic.Error, actual: ca1012DiagnosticOption);
                    }
        }
Esempio n. 3
0
        internal static MetadataReference CreateReflectionEmitAssembly(Action <ModuleBuilder> create)
        {
            using (var file = new DisposableFile(extension: ".dll"))
            {
                var name      = Path.GetFileName(file.Path);
                var appDomain = AppDomain.CurrentDomain;
                var assembly  = appDomain.DefineDynamicAssembly(new AssemblyName(name), AssemblyBuilderAccess.Save, Path.GetDirectoryName(file.Path));
                var module    = assembly.DefineDynamicModule(CommonTestBase.GetUniqueName(), name);
                create(module);
                assembly.Save(name);

                var image = CommonTestBase.ReadFromFile(file.Path);
                return(MetadataReference.CreateFromImage(image));
            }
        }
Esempio n. 4
0
        internal static MetadataReference CreateReflectionEmitAssembly(Action<ModuleBuilder> create)
        {
            using (var file = new DisposableFile(extension: ".dll"))
            {
                var name = Path.GetFileName(file.Path);
                var appDomain = AppDomain.CurrentDomain;
                var assembly = appDomain.DefineDynamicAssembly(new AssemblyName(name), AssemblyBuilderAccess.Save, Path.GetDirectoryName(file.Path));
                var module = assembly.DefineDynamicModule(CommonTestBase.GetUniqueName(), name);
                create(module);
                assembly.Save(name);

                var image = CommonTestBase.ReadFromFile(file.Path);
                return MetadataReference.CreateFromImage(image);
            }
        }
Esempio n. 5
0
        public void RuleSet_PathCanBeFound()
        {
            using (var ruleSetFile = new DisposableFile())
                using (var environment = new TestEnvironment())
                {
                    ProjectId projectId;

                    using (var project = CSharpHelpers.CreateCSharpCPSProject(environment, "Test"))
                    {
                        project.SetOptions($"/ruleset:{ruleSetFile.Path}");

                        projectId = project.Id;

                        Assert.Equal(ruleSetFile.Path, environment.Workspace.TryGetRuleSetPathForProject(projectId));
                    }

                    // Ensure it's still not available after we disposed the project
                    Assert.Null(environment.Workspace.TryGetRuleSetPathForProject(projectId));
                }
        }
Esempio n. 6
0
        public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath)
        {
            if (declarations == null)
            {
                throw new ArgumentNullException(nameof(declarations));
            }

            using (var sourceFile = new DisposableFile(extension: ".il"))
            {
                string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path);

                assemblyPath = Path.Combine(
                    TempRoot.Root,
                    Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll"));

                string completeIL;
                if (appendDefaultHeader)
                {
                    const string corLibName    = "mscorlib";
                    const string corLibVersion = "4:0:0:0";
                    const string corLibKey     = "B7 7A 5C 56 19 34 E0 89";

                    completeIL =
                        $@".assembly '{sourceFileName}' {{}} 

.assembly extern {corLibName} 
{{
  .publickeytoken = ({corLibKey})
  .ver {corLibVersion}
}} 

{declarations}";
                }
                else
                {
                    completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName);
                }

                sourceFile.WriteAllText(completeIL);

                var arguments = $"\"{sourceFile.Path}\" -DLL -out=\"{assemblyPath}\"";

                if (includePdb && !MonoHelpers.IsRunningOnMono())
                {
                    pdbPath    = Path.ChangeExtension(assemblyPath, "pdb");
                    arguments += string.Format(" -PDB=\"{0}\"", pdbPath);
                }
                else
                {
                    pdbPath = null;
                }

                var result = ProcessUtilities.Run(IlasmPath, arguments);

                if (result.ContainsErrors)
                {
                    throw new ArgumentException(
                              "The provided IL cannot be compiled." + Environment.NewLine +
                              IlasmPath + " " + arguments + Environment.NewLine +
                              result,
                              nameof(declarations));
                }
            }
        }
Esempio n. 7
0
        public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath)
        {
            if (declarations == null)
            {
                throw new ArgumentNullException(nameof(declarations));
            }

            using (var sourceFile = new DisposableFile(extension: ".il"))
            {
                string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path);

                assemblyPath = Path.Combine(
                    TempRoot.Root,
                    Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll"));

                string completeIL;
                if (appendDefaultHeader)
                {
                    completeIL = string.Format(
                        @".assembly '{0}' {{}} 

.assembly extern mscorlib 
{{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
  .ver 4:0:0:0
}} 

{1}",
                        sourceFileName,
                        declarations);
                }
                else
                {
                    completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName);
                }

                sourceFile.WriteAllText(completeIL);

                var ilasmPath = Path.Combine(
                    Path.GetDirectoryName(typeof(object).Assembly.Location),
                    "ilasm.exe");

                var arguments = string.Format(
                    "\"{0}\" /DLL /OUT=\"{1}\"",
                    sourceFile.Path,
                    assemblyPath);

                if (includePdb && !CLRHelpers.IsRunningOnMono())
                {
                    pdbPath    = Path.ChangeExtension(assemblyPath, "pdb");
                    arguments += string.Format(" /PDB=\"{0}\"", pdbPath);
                }
                else
                {
                    pdbPath = null;
                }

                var program = ilasmPath;
                if (CLRHelpers.IsRunningOnMono())
                {
                    arguments = string.Format("{0} {1}", ilasmPath, arguments);
                    arguments = arguments.Replace("\"", "");
                    arguments = arguments.Replace("=", ":");
                    program   = "mono";
                }

                var result = ProcessLauncher.Run(program, arguments);

                if (result.ContainsErrors)
                {
                    throw new ArgumentException(
                              "The provided IL cannot be compiled." + Environment.NewLine +
                              program + " " + arguments + Environment.NewLine +
                              result,
                              "declarations");
                }
            }
        }
Esempio n. 8
0
 public DisposableFile AddFile(DisposableFile file)
 {
     temps.Add(file);
     return file;
 }
        public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath)
        {
            if (declarations == null) throw new ArgumentNullException(nameof(declarations));

            using (var sourceFile = new DisposableFile(extension: ".il"))
            {
                string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path);

                assemblyPath = Path.Combine(
                    TempRoot.Root,
                    Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll"));

                string completeIL;
                if (appendDefaultHeader)
                {
                    completeIL = string.Format(
@".assembly '{0}' {{}} 

.assembly extern mscorlib 
{{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
  .ver 4:0:0:0
}} 

{1}",
                        sourceFileName,
                        declarations);
                }
                else
                {
                    completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName);
                }

                sourceFile.WriteAllText(completeIL);

                var ilasmPath = Path.Combine(
                    Path.GetDirectoryName(typeof(object).Assembly.Location),
                    "ilasm.exe");

                var arguments = string.Format(
                    "\"{0}\" /DLL /OUT=\"{1}\"",
                    sourceFile.Path,
                    assemblyPath);

                if (includePdb && !CLRHelpers.IsRunningOnMono())
                {
                    pdbPath = Path.ChangeExtension(assemblyPath, "pdb");
                    arguments += string.Format(" /PDB=\"{0}\"", pdbPath);
                }
                else
                {
                    pdbPath = null;
                }

                var program = ilasmPath;
                if (CLRHelpers.IsRunningOnMono())
                {
                    arguments = string.Format("{0} {1}", ilasmPath, arguments);
                    arguments = arguments.Replace("\"", "");
                    arguments = arguments.Replace("=", ":");
                    program = "mono";
                }

                var result = ProcessLauncher.Run(program, arguments);

                if (result.ContainsErrors)
                {
                    throw new ArgumentException(
                        "The provided IL cannot be compiled." + Environment.NewLine +
                        program + " " + arguments + Environment.NewLine +
                        result,
                        "declarations");
                }
            }
        }
Esempio n. 10
0
 public DisposableFile AddFile(DisposableFile file)
 {
     _temps.Add(file);
     return(file);
 }
Esempio n. 11
0
 public DisposableFile AddFile(DisposableFile file)
 {
     CheckDisposed();
     _temps.Add(file);
     return(file);
 }