Example #1
0
        /// <summary>
        /// Verify that if the DiscoveryTarget did not opt out of target
        /// filtering, that all TestInfos come from one of the expected areas, and
        /// that each of the expected areas is found in at least one TestInfo.
        /// If either of these is not true, trace an error.
        /// </summary>
        private static void VerifyAreaIndex(DiscoveryTarget discoveryTarget, IEnumerable <TestInfo> testInfosFromTarget)
        {
            IEnumerable <string> areasExpectedFromTarget = ComputeExpectedAreas(discoveryTarget);
            IEnumerable <string> areasDiscovered         = testInfosFromTarget.Select(testInfo => testInfo.Area).Distinct();

            if (!discoveryTarget.SkipTargetAreaFiltering)
            {
                // If any of the tests discovered came from an area other than one specified by the target, that is a violation.
                // This is super dangerous because it means that if we had been doing target filtering, this
                // case would have missed. This violation will always get caught in the lab.
                foreach (string areaDiscovered in areasDiscovered)
                {
                    if (!areasExpectedFromTarget.Contains(areaDiscovered))
                    {
                        Trace.TraceError("A test from area '{0}' was returned, which was not one of the possible areas specified by the target.", areaDiscovered);
                    }
                }

                // If for any of the areas specified by the target no tests were discovered for that area, that is a violation.
                // This isn't dangerous like above, but is wasteful since the target is being run unnecessarily.
                foreach (string areaExpected in areasExpectedFromTarget)
                {
                    if (!areasDiscovered.Contains(areaExpected))
                    {
                        Trace.TraceError("Target specified '{0}' as one of the areas it returns, which was not one of the actual returned test areas.", areaExpected);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Performs test case discovery from a DiscoveryTarget.
        /// </summary>
        /// <param name="discoveryTarget">Contains adaptor, manifest path, and default TestInfo data.</param>
        /// <param name="discoveryAdaptorType">Type DiscoveryTarget.Adaptor maps to.</param>
        /// <param name="discoveryRootDirectoryPath">Directory DiscoveryTarget.Path is relative to.</param>
        /// <param name="defaultTestInfo">Default to info to use as prototype.</param>
        /// <param name="filteringSettings">Filtering settings.</param>
        /// <returns>Collection of TestInfos.</returns>
        private static IEnumerable <TestInfo> Discover(DiscoveryTarget discoveryTarget, Type discoveryAdaptorType, DirectoryInfo discoveryRootDirectoryPath, TestInfo defaultTestInfo, FilteringSettings filteringSettings)
        {
            // Start with the default test info passed into the adaptor, and clone it.
            // then merge in local values from the discovery target's default test info.
            TestInfo compositedTestInfo = defaultTestInfo.Clone();

            compositedTestInfo.Merge(discoveryTarget.DefaultTestInfo);

            List <TestInfo> discoveredTests = new List <TestInfo>();

            DiscoveryAdaptor discoveryAdaptor = (DiscoveryAdaptor)Activator.CreateInstance(discoveryAdaptorType);

            IEnumerable <FileInfo> discoveryTargetFileNames = GetFiles(discoveryRootDirectoryPath, discoveryTarget.Path, compositedTestInfo, filteringSettings);

            foreach (FileInfo discoveryTargetFileName in discoveryTargetFileNames)
            {
                IEnumerable <TestInfo> discoveryTargetTests;
                try
                {
                    discoveryTargetTests = discoveryAdaptor.Discover(discoveryTargetFileName, compositedTestInfo);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("Exception when running " + discoveryAdaptor.GetType().Name + " against test manifest " + discoveryTargetFileName.FullName + ": " + e.Message);
                }
                discoveredTests.AddRange(discoveryTargetTests);
            }

            return(discoveredTests);
        }
Example #3
0
        private static IEnumerable <string> ComputeExpectedAreas(DiscoveryTarget discoveryTarget)
        {
            IEnumerable <string> expectedAreas = null;

            if (discoveryTarget.Areas != null)
            {
                expectedAreas = discoveryTarget.Areas;
            }
            // If the DiscoveryTarget doesn't specify an areas collection, we can filter
            // off of the Area value of its DefaultTestInfo.
            else if (discoveryTarget.DefaultTestInfo != null && !String.IsNullOrEmpty(discoveryTarget.DefaultTestInfo.Area))
            {
                expectedAreas = new string[] { discoveryTarget.DefaultTestInfo.Area };
            }
            else if (!discoveryTarget.SkipTargetAreaFiltering)
            {
                Trace.TraceError("Target for path '{0}' had filtering enabled, but the expected areas could not be determined because it did not specify a set of expected areas, nor did it specify a DefaultTestInfo with a default area value.", discoveryTarget.Path);
            }

            return(expectedAreas);
        }
Example #4
0
        private static bool ShouldRunTarget(DiscoveryTarget discoveryTarget, IEnumerable <string> areasToDiscover)
        {
            bool shouldRunTarget = false;
            IEnumerable <string> areasExpectedFromTarget = ComputeExpectedAreas(discoveryTarget);

            if (discoveryTarget.SkipTargetAreaFiltering)
            {
                shouldRunTarget = true;
            }
            else if (areasToDiscover == null)
            {
                shouldRunTarget = true;
            }
            else if (areasExpectedFromTarget == null)
            {
                shouldRunTarget = false;
            }
            else
            {
                shouldRunTarget = areasToDiscover.Any(areaToDiscover => areasExpectedFromTarget.ContainsSubstring(areaToDiscover, StringComparison.OrdinalIgnoreCase));
            }

            return(shouldRunTarget);
        }