Ejemplo n.º 1
0
        /// <summary>
        /// Creates a test run from a test collection - using the current tests in the collection
        /// as the source of tests to be executed. Changing the test collection after this call
        /// will not affect the created test run.
        /// </summary>
        /// <param name="testCollection">The test collection providing tests for the run.</param>
        /// <returns>The created test run.</returns>
        public static TestRun FromCollection(TestCollection testCollection)
        {
            if (testCollection == null)
            {
                throw new ArgumentNullException(nameof(testCollection));
            }

            var sourcesGroupedByDirectory = testCollection.Sources.GroupBy(Path.GetDirectoryName).Distinct();
            var sources = new List <string>();
            var temporaryDirectories = new List <string>();

            foreach (var directory in sourcesGroupedByDirectory)
            {
                var shadowcopyTargetDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                if (Directory.Exists(shadowcopyTargetDirectory))
                {
                    Directory.Delete(shadowcopyTargetDirectory, true);
                }

                CopyDirectory(directory.Key, shadowcopyTargetDirectory);
                temporaryDirectories.Add(shadowcopyTargetDirectory);

                foreach (var source in directory)
                {
                    var newSourcePath = source.Replace(directory.Key, shadowcopyTargetDirectory);
                    sources.Add(newSourcePath);
                }

                foreach (var dll in Directory.GetFiles(shadowcopyTargetDirectory, "*.dll", SearchOption.TopDirectoryOnly))
                {
                    var dllLower = Path.GetFileNameWithoutExtension(dll).ToLower();

                    if (dllLower != "jazsharp" && dllLower != "jazsharp.testadapter")
                    {
                        AssemblyRewriteHelper.RewriteAssembly(dll);
                    }
                }
            }

            var assemblyContext          = new AssemblyContext(temporaryDirectories.ToArray());
            var executionReadyAssemblies = sources.Select(assemblyContext.Load).ToList();

            assemblyContext.InitialiseDependencyResolvers();
            var jazSharpAssembly = assemblyContext.LoadByName(typeof(Jaz).Assembly.GetName());

            return
                (new TestRun(
                     assemblyContext,
                     testCollection.Tests.Select(x =>
                                                 x.Prepare(
                                                     executionReadyAssemblies.First(y => y.FullName == x.Execution.Main.Method.Module.Assembly.FullName),
                                                     jazSharpAssembly)),
                     temporaryDirectories));
        }