public static void TestTryGetTextFromUriFail()
        {
            Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;

            var webUrl =
                "https://gist.githubusercontent.com/ig-sinicyn/813e44fa231410892e480c717532bb77/raw/17537a4823e110460b02bf0de4ac11dbd8dcb763/SampleFile.badfile.txt";
            var webUrl2 =
                "https://gist.githubusercontent.baddomain/ig-sinicyn/813e44fa231410892e480c717532bb77/raw/17537a4823e110460b02bf0de4ac11dbd8dcb763/SampleFile.txt";
            var relativePath = "BadFile.txt";
            var fullPath     = Path.GetFullPath(relativePath);
            var fullPathUri  = new Uri(fullPath).ToString();

            IsNull(BenchmarkHelpers.TryGetTextFromUri(webUrl));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(webUrl2));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(relativePath));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(fullPath));
            IsNull(BenchmarkHelpers.TryGetTextFromUri(fullPathUri));
        }
        public static void TestTryGetTextFromUri()
        {
            Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;

            var webUrl =
                "https://gist.githubusercontent.com/ig-sinicyn/813e44fa231410892e480c717532bb77/raw/17537a4823e110460b02bf0de4ac11dbd8dcb763/SampleFile.txt";
            var relativePath = @"Assets\SampleFile.txt";
            var fullPath     = Path.GetFullPath(relativePath);
            var fullPathUri  = new Uri(fullPath).ToString();

            // ReSharper disable StringLiteralTypo
            var expectedText = @"A sample of text
Пример текста
テキストのサンプル";

            // ReSharper restore StringLiteralTypo
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(webUrl).ReadToEnd().Replace("\n", "\r\n"), expectedText);
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(relativePath).ReadToEnd(), expectedText);
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(fullPath).ReadToEnd(), expectedText);
            AreEqual(BenchmarkHelpers.TryGetTextFromUri(fullPathUri).ReadToEnd(), expectedText);
        }
Exemplo n.º 3
0
        public static XDocument[] TryParseXmlAnnotationDocsFromLog(
            [NotNull] string logUri,
            [NotNull] CompetitionState competitionState)
        {
            Code.NotNullNorEmpty(logUri, nameof(logUri));
            Code.NotNull(competitionState, nameof(competitionState));

            var result = new List <XDocument>();

            var logger = competitionState.Logger;

            logger.WriteLineInfo($"{LogVerbosePrefix} Downloading '{logUri}'.");

            using (var reader = BenchmarkHelpers.TryGetTextFromUri(logUri, TimeSpan.FromSeconds(15)))
            {
                if (reader == null)
                {
                    competitionState.WriteMessage(
                        MessageSource.Analyser, MessageSeverity.SetupError,
                        $"Could not load log content from '{logUri}'.");
                    return(Array <XDocument> .Empty);
                }

                logger.WriteLineInfo($"{LogVerbosePrefix} Downloaded '{logUri}'.");

                var    buffer = new StringBuilder();
                int    lineNumber = 0, xmlStartLineNumber = -1;
                string logLine;
                while ((logLine = reader.ReadLine()) != null)
                {
                    lineNumber++;

                    if (logLine.StartsWith(LogAnnotationStart, StringComparison.OrdinalIgnoreCase))
                    {
                        xmlStartLineNumber = lineNumber;
                    }
                    else if (logLine.StartsWith(LogAnnotationEnd, StringComparison.OrdinalIgnoreCase))
                    {
                        if (buffer.Length > 0)
                        {
                            var xmlAnnotationDoc = TryParseXmlAnnotationDocFromLogCore(
                                buffer.ToString(), competitionState, logUri, xmlStartLineNumber);
                            if (xmlAnnotationDoc != null)
                            {
                                result.Add(xmlAnnotationDoc);
                            }
                        }
                        buffer.Length = 0;

                        xmlStartLineNumber = -1;
                    }
                    else if (xmlStartLineNumber >= 0)
                    {
                        buffer.Append(logLine);
                    }
                }

                if (buffer.Length > 0)
                {
                    var xmlAnnotationDoc = TryParseXmlAnnotationDocFromLogCore(
                        buffer.ToString(), competitionState, logUri, xmlStartLineNumber);
                    if (xmlAnnotationDoc != null)
                    {
                        result.Add(xmlAnnotationDoc);
                    }
                }
            }

            return(result.ToArray());
        }