Exemple #1
0
        private void AddFile(ZipFile zipFile, ITestFile file, XElement parts)
        {
            AddFileToZip(zipFile, file.FileName, file.File);

            if ((Path.GetExtension(file.FileName) ?? string.Empty).Equals(".dll", StringComparison.OrdinalIgnoreCase))
            {
                /*
                //TODO: at a possible later time - understand if the pdb files have
                // value in a silverlight xap and how to truly leverage them.

                var pdbName = Path.Combine(Path.GetDirectoryName(file.FileName), Path.GetFileNameWithoutExtension(file.FileName), ".pdb");
                if (File.Exists(pdbName))
                {
                    AddFileToZip(zipFile, pdbName, File.ReadAllBytes(pdbName));
                    _logger.Debug("        including pdb - {0}".FormatWith(pdbName));
                }
                */

                var name = Path.GetFileNameWithoutExtension(file.FileName);

                if (string.IsNullOrEmpty(Path.GetDirectoryName(file.FileName)))
                {
                    parts.Add(new XElement("AssemblyPart",
                                           new XAttribute("StatLightTempName", name),
                                           new XAttribute("Source", file.FileName)));

                    _logger.Debug("        updated AppManifest - {0}".FormatWith(name));
                }
                else
                {
                    _logger.Debug("        Assembly not at root - not adding to AppManifest - {0}".FormatWith(name));
                }

            }
        }
Exemple #2
0
        private void AddFile(ZipFile zipFile, ITestFile file, XElement parts)
        {
            AddFileToZip(zipFile, file.FileName, file.File);

            if ((Path.GetExtension(file.FileName) ?? string.Empty).Equals(".dll", StringComparison.OrdinalIgnoreCase))
            {
                /*
                 * //TODO: at a possible later time - understand if the pdb files have
                 * // value in a silverlight xap and how to truly leverage them.
                 *
                 * var pdbName = Path.Combine(Path.GetDirectoryName(file.FileName), Path.GetFileNameWithoutExtension(file.FileName), ".pdb");
                 * if (File.Exists(pdbName))
                 * {
                 *  AddFileToZip(zipFile, pdbName, File.ReadAllBytes(pdbName));
                 *  _logger.Debug("        including pdb - {0}".FormatWith(pdbName));
                 * }
                 */

                var name = Path.GetFileNameWithoutExtension(file.FileName);

                if (string.IsNullOrEmpty(Path.GetDirectoryName(file.FileName)))
                {
                    parts.Add(new XElement("AssemblyPart",
                                           new XAttribute("StatLightTempName", name),
                                           new XAttribute("Source", file.FileName)));

                    _logger.Debug("        updated AppManifest - {0}".FormatWith(name));
                }
                else
                {
                    _logger.Debug("        Assembly not at root - not adding to AppManifest - {0}".FormatWith(name));
                }
            }
        }
        public IEnumerable <IHighlighting> GetHighlightings(ITestFile file)
        {
            var duplicatedTests = file.TestDeclarations.SelectMany(x => x.DescendantsAndSelf(y => y.TestDeclarations))
                                  .GroupBy(x => x.Identity)
                                  .Where(x => x.Count() > 1)
                                  .SelectMany(x => x).ToList();

            return(duplicatedTests.Select(x => new DuplicatedTestHighlighting(x)));
        }
        public IEnumerable<IHighlighting> GetHighlightings(ITestFile file)
        {
            var duplicatedTests = file.TestDeclarations.SelectMany (x => x.DescendantsAndSelf (y => y.TestDeclarations))
              .GroupBy (x => x.Identity)
              .Where (x => x.Count () > 1)
              .SelectMany (x => x).ToList ();

              return duplicatedTests.Select (x => new DuplicatedTestHighlighting (x));
        }
Exemple #5
0
        public ITestFile CreateActualFile(ITestFile expectedFile, string actualValue)
        {
            var fileName   = expectedFile.Name.Replace(".expected.txt", ".actual.txt");
            var fullName   = Path.Combine(_tempDirectoryForStringComparisons.Value.FullName, fileName);
            var actualFile = new FileInfo(fullName);

            actualFile.WriteAllText(actualValue);

            return(new TestFile(actualFile));
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="expectedFile"></param>
 /// <param name="actualFile"></param>
 /// <param name="innerException"></param>
 public DiffAssertException(
     ITestFile expectedFile,
     ITestFile actualFile,
     Exception innerException)
     : base(new StringBuilder()
            .AppendLine("Diff detected!")
            .AppendLine(expectedFile.FullName)
            .AppendLine(actualFile.FullName)
            .ToString(),
            innerException)
 {
 }
        public IEnumerable<IHighlighting> GetHighlightings(ITestFile file)
        {
            foreach (var classDeclaration in file.TestDeclarations)
              {
            var hasDefault = classDeclaration.DescendantsAndSelf (x => x.TestDeclarations).Any (x => x.Text == "<Default>");
            if (hasDefault)
              continue;

            classDeclaration.AssertIsValid ();
            yield return new NoDefaultTestHighlighting (classDeclaration);
              }
        }
        /// <summary>
        /// Starts the configured diff tool and uses the file names as arguments
        /// </summary>
        /// <param name="expected">The expected file (will be the left argument)</param>
        /// <param name="actual">The actual file (will be the right argument)</param>
        public void CompareFiles(ITestFile expected, ITestFile actual)
        {
            var arguments = string.Format(_diffToolArgsFormat, $"\"{expected.FullName}\"", $"\"{actual.FullName}\"");

            try
            {
                Process.Start(_diffToolPath, arguments);
            }
            catch (Exception ex)
            {
                throw new Exception($"Unable to launch diff tool {_diffToolPath} with arguments {arguments}. Check the paths and verify that the correct values are specified in the diff-assertions.json file.", ex);
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="expectedFile"></param>
 /// <param name="actualFile"></param>
 /// <param name="testRunnerException"></param>
 /// <param name="diffToolErrorMessage"></param>
 public DiffAssertException(
     ITestFile expectedFile,
     ITestFile actualFile,
     Exception testRunnerException,
     string diffToolErrorMessage)
     : base(new StringBuilder()
            .AppendLine("Diff detected!")
            .AppendLine(expectedFile.FullName)
            .AppendLine(actualFile.FullName)
            .AppendLine(diffToolErrorMessage)
            .ToString(),
            testRunnerException)
 {
 }
        public IEnumerable <IHighlighting> GetHighlightings(ITestFile file)
        {
            foreach (var classDeclaration in file.TestDeclarations)
            {
                var hasDefault = classDeclaration.DescendantsAndSelf(x => x.TestDeclarations).Any(x => x.Text == "<Default>");
                if (hasDefault)
                {
                    continue;
                }

                classDeclaration.AssertIsValid();
                yield return(new NoDefaultTestHighlighting(classDeclaration));
            }
        }
Exemple #11
0
        /// <summary>
        /// Called to test a C# codefix when applied on the inputted string as a source
        /// </summary>
        /// <param name="testBeforeFix">A test file belonging to a project that may contain markup and should contain diagnostics.</param>
        /// <param name="testAfterFix">A test file belonging to a project whose source should be "fixed".</param>
        /// <param name="additionalTestFiles"></param>
        /// <param name="additionalFiles">Supporting test files that will appear to the analyzer as "additional files".</param>
        /// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param>
        /// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param>
        protected void VerifyFix(
            ITestFile testBeforeFix,
            TestFile testAfterFix,
            IEnumerable <ITestFile> additionalTestFiles  = null,
            IEnumerable <AdditionalText> additionalFiles = null,
            int?codeFixIndex = null,
            bool allowNewCompilerDiagnostics = false)
        {
            var context = new JoinableTaskContext();

            new JoinableTaskFactory(context).Run(
                async() => await VerifyFixAsync(
                    DiagnosticAnalyzer,
                    CodeFixProvider,
                    testBeforeFix,
                    testAfterFix,
                    additionalTestFiles,
                    additionalFiles,
                    codeFixIndex,
                    allowNewCompilerDiagnostics));
        }
Exemple #12
0
        private void AddFile(IXapZipArchive xapZipArchive, ITestFile file, XElement parts)
        {
            AddFileInternal(xapZipArchive, file.FileName, file.File);

            if ((Path.GetExtension(file.FileName) ?? string.Empty).Equals(".dll", StringComparison.OrdinalIgnoreCase))
            {
                var name = Path.GetFileNameWithoutExtension(file.FileName);

                if (string.IsNullOrEmpty(Path.GetDirectoryName(file.FileName)))
                {
                    parts.Add(new XElement("AssemblyPart",
                                           new XAttribute("StatLightTempName", name),
                                           new XAttribute("Source", file.FileName)));

                    _logger.Debug("        updated AppManifest - {0}".FormatWith(name));
                }
                else
                {
                    _logger.Debug("        Assembly not at root - not adding to AppManifest - {0}".FormatWith(name));
                }
            }
        }
Exemple #13
0
        private void AddFile(IXapZipArchive xapZipArchive, ITestFile file, XElement parts)
        {
            AddFileInternal(xapZipArchive, file.FileName, file.File);

            if ((Path.GetExtension(file.FileName) ?? string.Empty).Equals(".dll", StringComparison.OrdinalIgnoreCase))
            {
                var name = Path.GetFileNameWithoutExtension(file.FileName);

                if (string.IsNullOrEmpty(Path.GetDirectoryName(file.FileName)))
                {
                    parts.Add(new XElement("AssemblyPart",
                                           new XAttribute("StatLightTempName", name),
                                           new XAttribute("Source", file.FileName)));

                    _logger.Debug("        updated AppManifest - {0}".FormatWith(name));
                }
                else
                {
                    _logger.Debug("        Assembly not at root - not adding to AppManifest - {0}".FormatWith(name));
                }
            }
        }
        private void InvokeDiffTool(
            ITestFile expectedFile,
            ITestFile actualFile,
            Exception testRunnerException)
        {
            try
            {
                _diffTool.CompareFiles(expectedFile, actualFile);
            }
            catch (Exception ex)
            {
                throw new DiffAssertException(
                          expectedFile,
                          actualFile,
                          testRunnerException,
                          ex.Message);
            }

            throw new DiffAssertException(
                      expectedFile,
                      actualFile,
                      testRunnerException);
        }
Exemple #15
0
        /// <summary>
        /// General verifier for codefixes.
        /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes.
        /// Then gets the string after the codefix is applied and compares it with the expected result.
        /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true.
        /// </summary>
        /// <param name="analyzer">The analyzer to be applied to the source code</param>
        /// <param name="codeFixProvider">The codefix to be applied to the code wherever the relevant Diagnostic is found</param>
        /// <param name="testBeforeFix">A test file belonging to a project that may contain markup and should contain diagnostics.</param>
        /// <param name="testAfterFix">A test file belonging to a project whose source should be "fixed".</param>
        /// <param name="additionalTestFiles"></param>
        /// <param name="additionalFiles">Supporting test files that will appear to the analyzer as "additional files".</param>
        /// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param>
        /// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param>
        private static async Task VerifyFixAsync(
            DiagnosticAnalyzer analyzer,
            CodeFixProvider codeFixProvider,
            ITestFile testBeforeFix,
            TestFile testAfterFix,
            IEnumerable <ITestFile> additionalTestFiles,
            IEnumerable <AdditionalText> additionalFiles,
            int?codeFixIndex,
            bool allowNewCompilerDiagnostics)
        {
            var testFiles = new ITestFile[] { testBeforeFix };

            testFiles = testFiles.Concat(additionalTestFiles ?? Enumerable.Empty <ITestFile>()).ToArray();
            var document            = GetDocuments(testFiles).First();
            var analyzerDiagnostics = (await GetSortedDiagnosticsAsync(
                                           analyzer,
                                           testFiles,
                                           additionalFiles: additionalFiles).ConfigureAwait(false)).ToList();
            var compilerDiagnostics = await GetCompilerDiagnosticsAsync(document).ConfigureAwait(false);

            var attempts = analyzerDiagnostics.Count;

            for (var i = 0; i < attempts; ++i)
            {
                var actions = new List <CodeAction>();
                var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
                await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);

                if (!actions.Any())
                {
                    break;
                }

                if (codeFixIndex != null)
                {
                    document = await ApplyFixAsync(document, actions.ElementAt((int)codeFixIndex)).ConfigureAwait(false);

                    break;
                }

                document = await ApplyFixAsync(document, actions.ElementAt(0)).ConfigureAwait(false);

                analyzerDiagnostics = (await GetSortedDiagnosticsAsync(
                                           analyzer,
                                           new[] { document },
                                           additionalFiles: additionalFiles).ConfigureAwait(false)).ToList();

                var compilerDiagnosticsNow = await GetCompilerDiagnosticsAsync(document).ConfigureAwait(false);

                var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, compilerDiagnosticsNow);

                // check if applying the code fix introduced any new compiler diagnostics
                if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any())
                {
                    // Format and get the compiler diagnostics again so that the locations make sense in the output
                    var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);

                    var updatedCompilerDiagnostics = await GetCompilerDiagnosticsAsync(document).ConfigureAwait(false);

                    document = document.WithSyntaxRoot(Formatter.Format(syntaxRoot, Formatter.Annotation, document.Project.Solution.Workspace));
                    newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, updatedCompilerDiagnostics);

                    Assert.True(false, string.Format(
                                    CultureInfo.InvariantCulture,
                                    "Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n",
                                    string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())),
                                    syntaxRoot.ToFullString()));
                }

                // check if there are analyzer diagnostics left after the code fix
                if (!analyzerDiagnostics.Any())
                {
                    break;
                }
            }

            // after applying all of the code fixes, compare the resulting string to the inputted one
            var actual = await GetStringFromDocumentAsync(document).ConfigureAwait(false);

            Assert.Equal(testAfterFix.Source, actual);

            // TODO: Enable if a codefix move the file into a different project
            // Assert.Equal(testAfterFix.ProjectName, document.Project.Name);
        }
 public TestFileDaemonStageProcess(IDaemonProcess process, ITestFile file, IEnumerable<ITestFileAnalyzer> testFileAnalyzers)
 {
     _process = process;
       _file = file;
       _testFileAnalyzers = testFileAnalyzers;
 }
Exemple #17
0
 public TestFileDaemonStageProcess(IDaemonProcess process, ITestFile file, IEnumerable <ITestFileAnalyzer> testFileAnalyzers)
 {
     DaemonProcess      = process;
     _file              = file;
     _testFileAnalyzers = testFileAnalyzers;
 }