Example #1
0
 internal void NotifyFinishedResults(TestResultsHolder oldResults)
 {
     OnTestResults.OnNext(oldResults);
 }
        public void OnMessage(ILongPollingConnection connection, string message, JToken data)
        {
            try
            {
                switch (message)
                {
                case "newClient":
                {
                    if (_verbose)
                    {
                        Console.WriteLine("New Test Client: " + data.Value <string>("userAgent"));
                    }
                    var client = UAParser.Parser.GetDefault().Parse(data.Value <string>("userAgent"));
                    lock (_lock)
                    {
                        _userAgent = client.ToString();
                        if (_url != null)
                        {
                            DoStart();
                        }
                        else
                        {
                            _connection.Send("wait", null);
                        }
                    }
                    _testServer.NotifySomeChange();
                    break;
                }

                case "wholeStart":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        _curResults.TotalTests = (int)data;
                        _suiteId    = 0;
                        _suiteStack = new Stack <SuiteOrTest>();
                        _suiteStack.Push(_curResults);
                    }
                    _testServer.NotifyTestingStarted();
                    _testServer.NotifySomeChange();
                    break;
                }

                case "wholeDone":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        _curResults.Duration = (double)data;
                        _curResults.Running  = false;
                        _oldResults          = _curResults;
                        _curResults          = null;
                        _suiteStack          = null;
                    }
                    _testServer.NotifyFinishedResults(_oldResults);
                    _testServer.NotifySomeChange();
                    break;
                }

                case "suiteStart":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var suite = new SuiteOrTest
                        {
                            Id       = ++_suiteId,
                            ParentId = _suiteStack.Peek().Id,
                            Name     = (string)data,
                            Nested   = new List <SuiteOrTest>(),
                            Duration = 0,
                            Failure  = false,
                            IsSuite  = true,
                            Failures = new List <MessageAndStack>(),
                            Skipped  = false,
                            Logs     = new List <MessageAndStack>()
                        };
                        _suiteStack.Peek().Nested.Add(suite);
                        _suiteStack.Push(suite);
                    }
                    _testServer.NotifySomeChange();
                    break;
                }

                case "suiteDone":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var suite = _suiteStack.Pop();
                        suite.Duration = data.Value <double>("duration");
                        suite.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures")));
                        if (suite.Failures.Count > 0)
                        {
                            suite.Failure = true;
                            foreach (var s in _suiteStack)
                            {
                                s.Failure = true;
                            }
                        }
                    }
                    _testServer.NotifySomeChange();
                    break;
                }

                case "testStart":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var test = new SuiteOrTest
                        {
                            Id       = ++_suiteId,
                            ParentId = _suiteStack.Peek().Id,
                            Name     = data.Value <string>("name"),
                            Stack    = ConvertMessageAndStack("", data.Value <string>("stack")).Stack.Where(f => f.FileName != "testbundle.js").ToList(),
                            Nested   = null,
                            Duration = 0,
                            Failure  = false,
                            IsSuite  = false,
                            Failures = new List <MessageAndStack>(),
                            Skipped  = false,
                            Logs     = new List <MessageAndStack>()
                        };
                        _suiteStack.Peek().Nested.Add(test);
                        _suiteStack.Push(test);
                    }
                    _testServer.NotifySomeChange();
                    break;
                }

                case "testDone":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var test = _suiteStack.Pop();
                        test.Duration = data.Value <double>("duration");
                        test.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures")));
                        _curResults.TestsFinished++;
                        var status = data.Value <string>("status");
                        if (status == "passed")
                        {
                        }
                        else if (status == "skipped" || status == "pending" || status == "disabled")
                        {
                            _curResults.TestsSkipped++;
                            test.Skipped = true;
                        }
                        else
                        {
                            _curResults.TestsFailed++;
                            test.Failure = true;
                            foreach (var s in _suiteStack)
                            {
                                s.Failure = true;
                            }
                        }
                    }
                    _testServer.NotifySomeChange();
                    break;
                }

                case "consoleLog":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var test = _suiteStack.Peek();
                        test.Logs.Add(ConvertMessageAndStack(data.Value <string>("message"), data.Value <string>("stack")));
                    }
                    _testServer.NotifySomeChange();
                    break;
                }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in TestServerConnectionHandler message: " + message);
                Console.WriteLine(ex.ToString());
            }
        }
Example #3
0
        void RunTest(TestCommand testCommand)
        {
            InitDiskCache();
            InitTestServer();
            InitMainServer();
            AddProject(PathUtils.Normalize(Environment.CurrentDirectory), testCommand.Sprite.Value);
            int port = 0;

            if (int.TryParse(testCommand.Port.Value, out var portInInt))
            {
                port = portInInt;
            }

            StartWebServer(port, false);
            DateTime start             = DateTime.UtcNow;
            int      errors            = 0;
            int      testFailures      = 0;
            int      warnings          = 0;
            var      messages          = new List <CompilationResultMessage>();
            var      messagesFromFiles = new HashSet <string>();
            var      totalFiles        = 0;

            foreach (var proj in _projects)
            {
                try
                {
                    _logger.WriteLine("Test build started " + proj.Owner.Owner.FullPath, ConsoleColor.Blue);
                    TestResultsHolder testResults = new TestResultsHolder();
                    proj.Owner.LoadProjectJson(true);
                    proj.Owner.InitializeOnce();
                    proj.StyleDefNaming = StyleDefNamingStyle.AddNames;
                    proj.GenerateCode();
                    proj.SpriterInitialization();
                    proj.RefreshMainFile();
                    proj.DetectBobrilJsxDts();
                    proj.RefreshTestSources();
                    if (proj.TestSources != null && proj.TestSources.Count > 0)
                    {
                        var ctx = new BuildCtx(_compilerPool, _verbose, ShowTsVersion);
                        ctx.TSCompilerOptions = proj.GetDefaultTSCompilerOptions();
                        ctx.Sources           = new HashSet <string>();
                        ctx.Sources.Add(proj.JasmineDts);
                        proj.TestSources.ForEach(s => ctx.Sources.Add(s));
                        if (proj.BobrilJsxDts != null)
                        {
                            ctx.Sources.Add(proj.BobrilJsxDts);
                        }
                        proj.Owner.Build(ctx);
                        var testBuildResult = ctx.BuildResult;
                        var fastBundle      = new FastBundleBundler(_tools);
                        var filesContent    = new Dictionary <string, object>();
                        proj.FillOutputByAdditionalResourcesDirectory(filesContent);
                        fastBundle.FilesContent = filesContent;
                        fastBundle.Project      = proj;
                        fastBundle.BuildResult  = testBuildResult;
                        fastBundle.Build("bb/base", "testbundle.js.map", true);
                        proj.TestProjFastBundle = fastBundle;
                        proj.FilesContent       = filesContent;
                        IncludeMessages(proj.TestProjFastBundle, ref errors, ref warnings, messages, messagesFromFiles,
                                        proj.Owner.Owner.FullPath);
                        if (errors == 0)
                        {
                            var wait = new Semaphore(0, 1);
                            _testServer.OnTestResults.Subscribe((results) =>
                            {
                                testFailures = results.TestsFailed;
                                testResults  = results;
                                wait.Release();
                            });
                            var durationb = DateTime.UtcNow - start;

                            _logger.Success("Build successful. Starting Chrome to run tests in " +
                                            durationb.TotalSeconds.ToString("F1", CultureInfo.InvariantCulture) + "s");

                            _testServer.StartTest("/test.html",
                                                  new Dictionary <string, SourceMap> {
                                { "testbundle.js", testBuildResult.SourceMap }
                            });
                            StartChromeTest();
                            wait.WaitOne();
                            StopChromeTest();
                        }
                    }

                    if (testCommand.Out.Value != null)
                    {
                        File.WriteAllText(testCommand.Out.Value,
                                          testResults.ToJUnitXml(testCommand.FlatTestSuites.Value), new UTF8Encoding(false));
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("Fatal Error: " + ex);
                    errors++;
                }
            }

            var duration = DateTime.UtcNow - start;
            var color    = (errors + testFailures) != 0 ? ConsoleColor.Red :
                           warnings != 0 ? ConsoleColor.Yellow : ConsoleColor.Green;

            _logger.WriteLine(
                "Test done in " + duration.TotalSeconds.ToString("F1", CultureInfo.InvariantCulture) + " with " +
                Plural(errors, "error") + " and " + Plural(warnings, "warning") + " and has " +
                Plural(totalFiles, "file") + " and " + Plural(testFailures, "failure"), color);

            Environment.ExitCode = (errors + testFailures) != 0 ? 1 : 0;
        }
 void InitCurResults()
 {
     _curResults = CreateEmptyResults();
 }
        public void OnMessage(ILongPollingConnection connection, string message, JToken data)
        {
            try
            {
                switch (message)
                {
                case "newClient":
                {
                    if (_verbose)
                    {
                        _logger.Info($"New Test Client: {data.Value<string>("userAgent")}");
                    }
                    var client = UAParser.Parser.GetDefault().Parse(data.Value <string>("userAgent"));
                    lock (_lock)
                    {
                        _userAgent = client.ToString();
                        if (_url != null)
                        {
                            DoStart();
                        }
                        else
                        {
                            _connection.Send("wait", null);
                        }
                    }

                    _testServer.NotifySomeChange();
                    break;
                }

                case "wholeStart":
                {
                    if (_verbose)
                    {
                        _logger.Info($"wholeStart tests:{(int)data}");
                    }
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        _curResults.TotalTests = (int)data;
                        _suiteId = 0;
                        if (_suiteStack == null)
                        {
                            _suiteStack = new Stack <SuiteOrTest>();
                            _suiteStack.Push(_curResults);
                        }
                    }

                    _testServer.NotifyTestingStarted();
                    _testServer.NotifySomeChange();
                    break;
                }

                case "wholeDone":
                {
                    if (_verbose)
                    {
                        _logger.Info($"wholeDone duration:{((double)data):f2}");
                    }
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        _curResults.Duration = (double)data;
                        _curResults.Running  = false;
                        _oldResults          = _curResults;
                        _curResults          = null;
                        _suiteStack          = null;
                    }

                    _testServer.NotifyFinishedResults(_oldResults);
                    _testServer.NotifySomeChange();
                    break;
                }

                case "suiteStart":
                {
                    if (_verbose)
                    {
                        _logger.Info($"suiteStart {(string)data}");
                    }
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var suite = new SuiteOrTest
                        {
                            Id       = ++_suiteId,
                            ParentId = _suiteStack.Peek().Id,
                            Name     = (string)data,
                            Nested   = new List <SuiteOrTest>(),
                            Duration = 0,
                            Failure  = false,
                            IsSuite  = true,
                            Failures = new List <MessageAndStack>(),
                            Skipped  = false,
                            Logs     = new List <MessageAndStack>()
                        };
                        _suiteStack.Peek().Nested.Add(suite);
                        _suiteStack.Push(suite);
                    }

                    _testServer.NotifySomeChange();
                    break;
                }

                case "suiteDone":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var suite = _suiteStack.Pop();
                        suite.Duration = data.Value <double>("duration");
                        if (_verbose)
                        {
                            _logger.Info($"suiteDone {suite.Name} {suite.Duration:f2}");
                        }
                        suite.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures")));
                        if (suite.Failures.Count > 0)
                        {
                            _curResults.SuitesFailed += suite.Failures.Count;
                            suite.Failure             = true;
                            _logger.Error(
                                $"suite {suite.Name} in between test failures\n{string.Join('\n', suite.Failures.Select(f => f.Message + "\n  " + string.Join("\n  ", f.Stack)))}");
                            foreach (var s in _suiteStack)
                            {
                                s.Failure = true;
                            }
                        }
                    }

                    _testServer.NotifySomeChange();
                    break;
                }

                case "testStart":
                {
                    if (_verbose)
                    {
                        _logger.Info("testStart " + data.Value <string>("name"));
                    }
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var test = new SuiteOrTest
                        {
                            Id       = ++_suiteId,
                            ParentId = _suiteStack.Peek().Id,
                            Name     = data.Value <string>("name"),
                            Stack    = ConvertMessageAndStack("", data.Value <string>("stack")).Stack
                                       .Where(f => f.FileName != "bundle.js").ToList(),
                            Nested   = null,
                            Duration = 0,
                            Failure  = false,
                            IsSuite  = false,
                            Failures = new List <MessageAndStack>(),
                            Skipped  = false,
                            Logs     = new List <MessageAndStack>()
                        };
                        _suiteStack.Peek().Nested.Add(test);
                        _suiteStack.Push(test);
                    }

                    _testServer.NotifySomeChange();
                    break;
                }

                case "testDone":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var test = _suiteStack.Pop();
                        test.Duration = data.Value <double>("duration");
                        test.Failures.AddRange(ConvertFailures(data.Value <JArray>("failures")));
                        _curResults.TestsFinished++;
                        var status = data.Value <string>("status");
                        if (_verbose)
                        {
                            _logger.Info("testDone " + test.Name + " " + status);
                        }
                        if (status == "passed")
                        {
                        }
                        else if (status == "skipped" || status == "pending" || status == "disabled" || status == "excluded")
                        {
                            _curResults.TestsSkipped++;
                            test.Skipped = true;
                        }
                        else
                        {
                            _curResults.TestsFailed++;
                            test.Failure = true;
                            foreach (var s in _suiteStack)
                            {
                                s.Failure = true;
                            }
                        }
                    }

                    _testServer.NotifySomeChange();
                    break;
                }

                case "consoleLog":
                {
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        if (_suiteStack == null)
                        {
                            break;
                        }
                        var test = _suiteStack.Peek();
                        test.Logs.Add(ConvertMessageAndStack(data.Value <string>("message"),
                                                             data.Value <string>("stack")));
                    }

                    _testServer.NotifySomeChange();
                    break;
                }

                case "onerror":
                {
                    if (_verbose)
                    {
                        _logger.Error("onerror " + data);
                    }
                    lock (_lock)
                    {
                        if (_curResults == null)
                        {
                            break;
                        }
                        _suiteId    = 0;
                        _suiteStack = new Stack <SuiteOrTest>();
                        _suiteStack.Push(_curResults);
                        _curResults.Failures.Add(ConvertMessageAndStack(data.Value <string>("message"), data.Value <string>("stack")));
                        _logger.Error("Test onerror " + _curResults.Failures[^ 1].Message);