예제 #1
0
        private void RunTestSuite(ITestResultLogger logger, string directoryName)
        {
            var project = new Project(Path.Combine(directoryName, Path.GetFileName(directoryName) + ".unoproj"));

            project.MutablePackageReferences.Add(new PackageReference(project.Source, "Uno.Testing"));
            project.MutableProjectReferences.Add(new ProjectReference(project.Source, "../_Outracks.UnoTest.InternalHelpers/_Outracks.UnoTest.InternalHelpers.unoproj"));

            logger.ProjectStarting(project.Name, BuildTargets.Default.Identifier);
            var tests = new List <Test>();

            foreach (var file in Directory.GetFiles(directoryName, "*.uno").Where(x => x.EndsWith(".uno")))
            {
                var filePath = Path.GetFullPath(file);
                project.MutableIncludeItems.Clear();
                var fileName = Path.GetFileName(filePath);
                project.MutableIncludeItems.Add(new IncludeItem(project.Source, IncludeItemType.Source, fileName));

                var test = new Test(fileName);
                tests.Add(test);

                var expectations = ParseAssertComment(filePath).ToList();
                var ignores      = expectations.Where(e => e.Type == ErrorType.Ignore).ToList();
                expectations = expectations.Except(ignores).ToList();

                foreach (var ignore in ignores)
                {
                    logger.TestIgnored(new Test(ignore.ToString()));
                }

                var output           = RunBuild(project);
                var errors           = ParseOutput(output);
                var unexpectedErrors = errors.Except(expectations, new ErrorItemComparer()).ToList();
                var notGivenErrors   = expectations.Except(errors, new ErrorItemComparer()).ToList();

                foreach (var error in unexpectedErrors)
                {
                    test.Asserted(new Assertion(error.Source.FullPath, error.Source.Line, null, "(Got an unexpected error)", error.ToString(), error.Expression));
                    logger.TestAsserted(test);
                }

                foreach (var error in notGivenErrors)
                {
                    test.Asserted(new Assertion(error.Source.FullPath, error.Source.Line, null, error.ToString(), "(Missed an expected error)", error.Expression));
                    logger.TestAsserted(test);
                }

                if (!unexpectedErrors.Any() && !notGivenErrors.Any())
                {
                    test.Passed();
                    logger.TestPassed(test);
                }
            }

            logger.ProjectEnded(tests);
            logger.Log("");
        }
예제 #2
0
        public List <Test> RunTests()
        {
            var project = Path.GetFileNameWithoutExtension(_unoProj);

            _logger.ProjectStarting(project, _options.Target.ToString());
            List <Test> tests = new List <Test>();

            try
            {
                _testRun = new TestRun(_logger);

                var  cts         = new CancellationTokenSource();
                bool runFinished = false;
                try
                {
                    var log             = Log.Default;
                    var target          = _options.Target;
                    var proj            = Project.Load(_unoProj);
                    var outputDirectory = _options.OutputDirectory ?? Path.GetFullPath(Path.Combine(proj.BuildDirectory, "Test", target.Identifier));

                    var options = new BuildOptions {
                        Test            = true,
                        Force           = true,
                        TestFilter      = _options.Filter,
                        OutputDirectory = outputDirectory,
                        WarningLevel    = 1,
                        Library         = _options.Library,
                        PackageTarget   = Build.Targets.BuildTargets.Package
                    };

                    options.Defines.AddRange(_options.Defines);
                    options.Undefines.AddRange(_options.Undefines);

                    var builder = new ProjectBuilder(log, target, options);
                    var result  = builder.Build(proj);
                    if (result.ErrorCount != 0)
                    {
                        throw new Exception("Build failed.");
                    }
                    if (_options.OnlyBuild)
                    {
                        return(tests);
                    }

                    // We don't need a window when running tests.
                    Environment.SetEnvironmentVariable("UNO_WINDOW_HIDDEN", "1");

                    var targetLog = new Log(new DebugLogTestFilter(Console.Out, _testRun), Console.Error);

                    Task runTask = null;
                    try
                    {
                        runTask = Task.Run(() => result.RunAsync(targetLog, cts.Token), cts.Token);
                        _testRun.Start();

                        tests       = _testRun.WaitUntilFinished();
                        runFinished = runTask.Wait(100);
                    }
                    finally
                    {
                        if ((target is AndroidBuild || target is iOSBuild) &&
                            !_options.NoUninstall &&
                            runTask != null)
                        {
                            // Wait a little more for app to quit, after that we don't care
                            runTask.Wait(500);
                            Task.Run(() => target.Run(Shell.Default, result.File, "uninstall", cts.Token)).Wait();
                        }
                    }
                }
                finally
                {
                    _logger.ProjectEnded(tests);
                    if (!runFinished)
                    {
                        cts.Cancel();
                    }
                    cts.Dispose();
                }
            }
            finally
            {
                if (_testRun != null)
                {
                    _testRun.Dispose();
                    _testRun = null;
                }
            }
            return(tests);
        }
예제 #3
0
        public List <Test> RunTests()
        {
            var project = Path.GetFileNameWithoutExtension(_unoProj);

            _logger.ProjectStarting(project, _options.Target.ToString());
            List <Test> tests = new List <Test>();

            try
            {
                _testRun = new TestRun(_logger, _options.TestTimeout, _options.StartupTimeout);

                HttpTestCommunicator communicator = null;
                if (!_options.RunLocal)
                {
                    communicator = new HttpTestCommunicator(_logger, _testRun, NeedsPublicIp());
                }

                var  cts         = new CancellationTokenSource();
                bool runFinished = false;
                try
                {
                    var log             = Log.Default;
                    var target          = _options.Target;
                    var proj            = Project.Load(_unoProj);
                    var outputDirectory = _options.OutputDirectory ?? Path.GetFullPath(Path.Combine(proj.BuildDirectory, "Test", target.Identifier));

                    // YUCK: need to start the communicator before building, to get the Prefix/TestServerUrl
                    if (communicator != null)
                    {
                        communicator.Start();
                    }

                    var options = new BuildOptions {
                        Test            = true,
                        Force           = true,
                        TestFilter      = _options.Filter,
                        TestServerUrl   = _options.RunLocal ? string.Empty : communicator.Prefix,
                        OutputDirectory = outputDirectory,
                        WarningLevel    = 1,
                        Library         = _options.Library,
                        PackageTarget   = Build.Targets.BuildTargets.Package
                    };

                    options.Defines.AddRange(_options.Defines);
                    options.Undefines.AddRange(_options.Undefines);

                    if (_options.OpenDebugger)
                    {
                        options.RunArguments = "debug";
                        options.Native       = false;        // disable native build
                        options.Defines.Add("DEBUG_NATIVE"); // disable native optimizations
                    }

                    var builder = new ProjectBuilder(log, target, options);
                    var result  = builder.Build(proj);
                    if (result.ErrorCount != 0)
                    {
                        throw new Exception("Build failed.");
                    }

                    Log targetLog = null;
                    if (_options.RunLocal)
                    {
                        targetLog = new Log(new DebugLogTestFilter(Console.Out, _testRun), Console.Error);
                    }
                    Task runTask = null;
                    try
                    {
                        if (!_options.AllowDebugger)
                        {
                            runTask = Task.Run(() => result.RunAsync(targetLog, cts.Token), cts.Token);
                        }
                        _testRun.Start();


                        tests = _testRun.WaitUntilFinished();
                        if (runTask != null)
                        {
                            runFinished = runTask.Wait(100);
                        }
                    }
                    finally
                    {
                        if ((target is AndroidBuild || target is iOSBuild) &&
                            !_options.NoUninstall &&
                            runTask != null)
                        {
                            // Wait a little more for app to quit, after that we don't care
                            runTask.Wait(500);
                            Task.Run(() => target.Run(Shell.Default, result.File, "uninstall", cts.Token)).Wait();
                        }
                    }
                }
                finally
                {
                    if (communicator != null)
                    {
                        communicator.Stop();
                    }

                    _logger.ProjectEnded(tests);
                    if (!runFinished)
                    {
                        cts.Cancel();
                    }
                    cts.Dispose();
                }
            }
            finally
            {
                if (_testRun != null)
                {
                    _testRun.Dispose();
                    _testRun = null;
                }
            }
            return(tests);
        }