public FilteredTestDriver(
     DefaultTestFrameworkManager testFrameworkManager,
     IEnumerable <ComponentHandle <ITestFramework, TestFrameworkTraits> > testFrameworkHandles,
     TestFrameworkFallbackMode testFrameworkFallbackMode, TestFrameworkOptions testFrameworkOptions,
     ILogger logger)
 {
     this.testFrameworkManager      = testFrameworkManager;
     this.testFrameworkHandles      = testFrameworkHandles;
     this.testFrameworkFallbackMode = testFrameworkFallbackMode;
     this.testFrameworkOptions      = testFrameworkOptions;
     this.logger = logger;
 }
        SelectTestFrameworksForCodeElementsImpl(
            IEnumerable <ComponentHandle <ITestFramework, TestFrameworkTraits> > filteredTestFrameworkHandlesWithoutFallback,
            TestFrameworkFallbackMode testFrameworkFallbackMode,
            TestFrameworkOptions testFrameworkOptions,
            IEnumerable <ICodeElementInfo> codeElements)
        {
            var selections = new Dictionary <object, TestFrameworkSelection>();
            var partitions = new MultiMap <TestFrameworkSelection, ICodeElementInfo>();

            foreach (var codeElement in codeElements)
            {
                IAssemblyInfo assembly = ReflectionUtils.GetAssembly(codeElement);
                if (assembly != null)
                {
                    IList <AssemblyName> assemblyReferences = assembly.GetReferencedAssemblies();

                    bool supported = false;
                    foreach (var testFrameworkHandle in filteredTestFrameworkHandlesWithoutFallback)
                    {
                        if (testFrameworkHandle.GetTraits().IsFrameworkCompatibleWithAssemblyReferences(assemblyReferences))
                        {
                            TestFrameworkSelection selection = GetOrCreateSelectionIfAbsent(
                                selections, testFrameworkHandle,
                                () => new TestFrameworkSelection(testFrameworkHandle, testFrameworkOptions, false));

                            partitions.Add(selection, codeElement);
                            supported = true;
                        }
                    }

                    if (!supported)
                    {
                        TestFrameworkSelection selection = GetFallbackSelection(selections, assemblyReferences,
                                                                                filteredTestFrameworkHandlesWithoutFallback, testFrameworkOptions, testFrameworkFallbackMode);
                        if (selection != null)
                        {
                            partitions.Add(selection, codeElement);
                        }
                    }
                }
            }

            return(partitions);
        }
 /// <summary>
 /// Resets <see cref="TestFrameworkFallbackMode"/> to its default value and sets <see cref="IsTestFrameworkFallbackModeSpecified" /> to false.
 /// </summary>
 public void ResetTestFrameworkFallbackMode()
 {
     testFrameworkFallbackMode = TestFrameworkFallbackMode.Default;
     isTestFrameworkFallbackModeSpecified = false;
 }
        /// <summary>
        /// Merges the specified package to the current one.
        /// </summary>
        /// <param name="other">The other package to be merged.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="other"/> is null.</exception>
        public void MergeWith(TestPackage other)
        {
            if (other == null)
                throw new ArgumentNullException("other");

            if (other.isTestFrameworkOptionsSpecified)
            {
                isTestFrameworkOptionsSpecified = true;
                testFrameworkOptions.Properties.MergeWith(other.TestFrameworkOptions.Properties);
            }

            if (other.isTestFrameworkFallbackModeSpecified && !isTestFrameworkFallbackModeSpecified)
            {
                isTestFrameworkFallbackModeSpecified = true;
                testFrameworkFallbackMode = other.testFrameworkFallbackMode;
            }

            if (other.isShadowCopySpecified && !isShadowCopySpecified)
            {
                isShadowCopySpecified = true;
                shadowCopy |= other.shadowCopy;
            }

            if (other.isDebuggerSetupSpecified)
            {
                isDebuggerSetupSpecified = true;

                if (other.debuggerSetup != null)
                {
                    if (debuggerSetup == null)
                        debuggerSetup = new DebuggerSetup();

                    debuggerSetup.Properties.MergeWith(other.debuggerSetup.Properties);
                }
            }

            if (other.isApplicationBaseDirectorySpecified && !isApplicationBaseDirectorySpecified)
            {
                isApplicationBaseDirectorySpecified = true;
                applicationBaseDirectory = other.applicationBaseDirectory;
            }

            if (other.isWorkingDirectorySpecified && !isWorkingDirectorySpecified)
            {
                isWorkingDirectorySpecified = true;
                workingDirectory = other.workingDirectory;
            }

            if (other.isRuntimeVersionSpecified && !isRuntimeVersionSpecified)
            {
                isRuntimeVersionSpecified = true;
                runtimeVersion = other.runtimeVersion;
            }

            foreach (string item in other.excludedTestFrameworkIds)
            {
                if (!excludedTestFrameworkIds.Contains(item))
                    excludedTestFrameworkIds.Add(item);
            }

            foreach (FileInfo item in other.files)
            {
                if (files.FindIndex(i => i.FullName == item.FullName) < 0)
                    files.Add(item);
            }

            foreach (DirectoryInfo item in other.hintDirectories)
            {
                if (hintDirectories.FindIndex(i => i.FullName == item.FullName) < 0)
                    hintDirectories.Add(item);
            }

            properties.MergeWith(other.properties);
        }
Exemple #5
0
 /// <summary>
 /// Resets <see cref="TestFrameworkFallbackMode"/> to its default value and sets <see cref="IsTestFrameworkFallbackModeSpecified" /> to false.
 /// </summary>
 public void ResetTestFrameworkFallbackMode()
 {
     testFrameworkFallbackMode            = TestFrameworkFallbackMode.Default;
     isTestFrameworkFallbackModeSpecified = false;
 }
Exemple #6
0
        /// <summary>
        /// Merges the specified package to the current one.
        /// </summary>
        /// <param name="other">The other package to be merged.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="other"/> is null.</exception>
        public void MergeWith(TestPackage other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            if (other.isTestFrameworkOptionsSpecified)
            {
                isTestFrameworkOptionsSpecified = true;
                testFrameworkOptions.Properties.MergeWith(other.TestFrameworkOptions.Properties);
            }

            if (other.isTestFrameworkFallbackModeSpecified && !isTestFrameworkFallbackModeSpecified)
            {
                isTestFrameworkFallbackModeSpecified = true;
                testFrameworkFallbackMode            = other.testFrameworkFallbackMode;
            }

            if (other.isShadowCopySpecified && !isShadowCopySpecified)
            {
                isShadowCopySpecified = true;
                shadowCopy           |= other.shadowCopy;
            }

            if (other.isDebuggerSetupSpecified)
            {
                isDebuggerSetupSpecified = true;

                if (other.debuggerSetup != null)
                {
                    if (debuggerSetup == null)
                    {
                        debuggerSetup = new DebuggerSetup();
                    }

                    debuggerSetup.Properties.MergeWith(other.debuggerSetup.Properties);
                }
            }

            if (other.isApplicationBaseDirectorySpecified && !isApplicationBaseDirectorySpecified)
            {
                isApplicationBaseDirectorySpecified = true;
                applicationBaseDirectory            = other.applicationBaseDirectory;
            }

            if (other.isWorkingDirectorySpecified && !isWorkingDirectorySpecified)
            {
                isWorkingDirectorySpecified = true;
                workingDirectory            = other.workingDirectory;
            }

            if (other.isRuntimeVersionSpecified && !isRuntimeVersionSpecified)
            {
                isRuntimeVersionSpecified = true;
                runtimeVersion            = other.runtimeVersion;
            }

            foreach (string item in other.excludedTestFrameworkIds)
            {
                if (!excludedTestFrameworkIds.Contains(item))
                {
                    excludedTestFrameworkIds.Add(item);
                }
            }

            foreach (FileInfo item in other.files)
            {
                if (files.FindIndex(i => i.FullName == item.FullName) < 0)
                {
                    files.Add(item);
                }
            }

            foreach (DirectoryInfo item in other.hintDirectories)
            {
                if (hintDirectories.FindIndex(i => i.FullName == item.FullName) < 0)
                {
                    hintDirectories.Add(item);
                }
            }

            properties.MergeWith(other.properties);
        }
        private TestFrameworkSelection GetFallbackSelection(
            Dictionary <object, TestFrameworkSelection> selections,
            IList <AssemblyName> assemblyReferences,
            IEnumerable <ComponentHandle <ITestFramework, TestFrameworkTraits> > filteredTestFrameworkHandlesWithoutFallback,
            TestFrameworkOptions testFrameworkOptions,
            TestFrameworkFallbackMode testFrameworkFallbackMode)
        {
            // Strict fallback mode.
            if (testFrameworkFallbackMode == TestFrameworkFallbackMode.Strict)
            {
                return(null);
            }

            // Approximate fallback mode.
            if (assemblyReferences != null)
            {
                HashSet <string> matchingReferences = null;
                HashSet <string> matchingSignatures = null;

                foreach (var testFrameworkHandle in filteredTestFrameworkHandlesWithoutFallback)
                {
                    IList <AssemblySignature> assemblySignatures = testFrameworkHandle.GetTraits().FrameworkAssemblies;

                    foreach (AssemblyName assemblyName in assemblyReferences)
                    {
                        foreach (AssemblySignature assemblySignature in assemblySignatures)
                        {
                            if (assemblySignature.Name == assemblyName.Name)
                            {
                                if (matchingReferences == null)
                                {
                                    matchingReferences = new HashSet <string>();
                                    matchingSignatures = new HashSet <string>();
                                }

                                matchingReferences.Add(assemblyName.FullName);
                                matchingSignatures.Add(assemblySignature.ToString());
                            }
                        }
                    }
                }

                if (matchingReferences != null)
                {
                    StringBuilder fallbackExplanationBuilder = new StringBuilder();

                    fallbackExplanationBuilder.Append("Detected a probable test framework assembly version mismatch.\n");

                    fallbackExplanationBuilder.Append("Referenced test frameworks: ");
                    AppendQuotedItems(fallbackExplanationBuilder, matchingReferences);
                    fallbackExplanationBuilder.Append(".\n");

                    fallbackExplanationBuilder.Append("Supported test frameworks: ");
                    AppendQuotedItems(fallbackExplanationBuilder, matchingSignatures);
                    fallbackExplanationBuilder.Append(".");

                    string fallbackExplanation = fallbackExplanationBuilder.ToString();
                    return(GetOrCreateSelectionIfAbsent(selections, fallbackExplanation, () =>
                    {
                        TestFrameworkOptions fallbackTestFrameworkOptions = testFrameworkOptions.Copy();
                        fallbackTestFrameworkOptions.AddProperty(FallbackTestFramework.FallbackExplanationKey,
                                                                 fallbackExplanation);
                        return new TestFrameworkSelection(fallbackTestFrameworkHandle, fallbackTestFrameworkOptions,
                                                          true);
                    }));
                }
            }

            if (testFrameworkFallbackMode == TestFrameworkFallbackMode.Approximate)
            {
                return(null);
            }

            // Default fallback mode.
            return(GetOrCreateSelectionIfAbsent(selections, DefaultFallbackSelectionKey,
                                                () => new TestFrameworkSelection(fallbackTestFrameworkHandle, testFrameworkOptions, true)));
        }
        SelectTestFrameworksForFilesImpl(
            IEnumerable <ComponentHandle <ITestFramework, TestFrameworkTraits> > filteredTestFrameworkHandlesWithoutFallback,
            TestFrameworkFallbackMode testFrameworkFallbackMode,
            TestFrameworkOptions testFrameworkOptions,
            IEnumerable <FileInfo> files)
        {
            var selections = new Dictionary <object, TestFrameworkSelection>();
            var partitions = new MultiMap <TestFrameworkSelection, FileInfo>();

            foreach (var file in files)
            {
                try
                {
                    bool supported = false;
                    IList <AssemblyName> assemblyReferences = null;
                    using (var fileInspector = new LazyFileInspector(file))
                    {
                        FileType fileType = fileTypeManager.IdentifyFileType(fileInspector);

                        foreach (var testFrameworkHandle in filteredTestFrameworkHandlesWithoutFallback)
                        {
                            TestFrameworkTraits traits = testFrameworkHandle.GetTraits();
                            if (!traits.IsFrameworkCompatibleWithFileType(fileType, fileTypeManager))
                            {
                                continue;
                            }

                            // If the file is an assembly, then filter further by assembly references.
                            if (fileType.IsSameOrSubtypeOf(fileTypeManager.GetFileTypeById("Assembly")))
                            {
                                if (assemblyReferences == null)
                                {
                                    assemblyReferences = GetAssemblyReferences(fileInspector);
                                }

                                if (!traits.IsFrameworkCompatibleWithAssemblyReferences(assemblyReferences))
                                {
                                    continue;
                                }
                            }

                            TestFrameworkSelection selection = GetOrCreateSelectionIfAbsent(
                                selections, testFrameworkHandle,
                                () => new TestFrameworkSelection(testFrameworkHandle, testFrameworkOptions, false));

                            partitions.Add(selection, file);
                            supported = true;
                        }
                    }

                    if (!supported)
                    {
                        TestFrameworkSelection selection = GetFallbackSelection(selections, assemblyReferences,
                                                                                filteredTestFrameworkHandlesWithoutFallback, testFrameworkOptions, testFrameworkFallbackMode);
                        if (selection != null)
                        {
                            partitions.Add(selection, file);
                        }
                    }
                }
                catch (IOException exception)
                {
                    logger.Log(LogSeverity.Error, string.Format("Skipping file '{0}' due to IO error.", file.FullName), exception);
                }
                catch (UnauthorizedAccessException exception)
                {
                    logger.Log(LogSeverity.Error, string.Format("Skipping file '{0}' due to security error", file.FullName), exception);
                }
            }

            return(partitions);
        }