<commit_before>﻿/*
 * Copyright (c) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

using System;
using Google.Logging.V2;
using Xunit;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace GoogleCloudSamples
{
    public class BaseTest
    {
        private readonly string _projectId;
        private readonly List<string> _logsToDelete = new List<string>();
        private readonly List<string> _sinksToDelete = new List<string>();

        public BaseTest()
        {
            _projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
        }

        public struct ConsoleOutput
        {
            public int ExitCode;
            public string Stdout;
        };

        /// <summary>Runs LoggingSample.exe with the provided arguments</summary>
        /// <returns>The console output of this program</returns>
        public static ConsoleOutput Run(params string[] arguments)
        {
            Console.Write("LoggingSample.exe ");
            Console.WriteLine(string.Join(" ", arguments));

            using (var output = new StringWriter())
            {
                LoggingSample loggingSample = new LoggingSample(output);
                var consoleOutput = new ConsoleOutput()
                {
                    ExitCode = loggingSample.Run(arguments),
                    Stdout = output.ToString()
                };
                Console.Write(consoleOutput.Stdout);
                return consoleOutput;
            }
        }

        protected static void AssertSucceeded(ConsoleOutput output)
        {
            Assert.True(0 == output.ExitCode,
                $"Exit code: {output.ExitCode}\n{output.Stdout}");
        }

        public class LoggingTest : BaseTest, IDisposable
        {
            public void Dispose()
            {
                try
                {
                    // Delete all logs created from running the tests.
                    foreach (string log in logsToDelete)
                    {
                        Run("delete-log", log);
                    }
                }
                catch (RpcException ex)
                {
                    // Not Found exceptions are allowed otherwise throw.
                    if (ex.Status.StatusCode != StatusCode.NotFound)
                    {
                        throw;
                    }
                }
                try
                {
                    // Delete all the log sinks created from running the tests.
                    foreach (string sink in sinksToDelete)
                    {
                        Run("delete-sink", sink);<commit_msg>Add using directives<commit_after>﻿/*
 * Copyright (c) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

using System;
using Google.Logging.V2;
using Xunit;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;
using Grpc.Core;

namespace GoogleCloudSamples
{
    public class BaseTest
    {
        private readonly string _projectId;
        private readonly List<string> _logsToDelete = new List<string>();
        private readonly List<string> _sinksToDelete = new List<string>();

        public BaseTest()
        {
            _projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
        }

        public struct ConsoleOutput
        {
            public int ExitCode;
            public string Stdout;
        };

        /// <summary>Runs LoggingSample.exe with the provided arguments</summary>
        /// <returns>The console output of this program</returns>
        public static ConsoleOutput Run(params string[] arguments)
        {
            Console.Write("LoggingSample.exe ");
            Console.WriteLine(string.Join(" ", arguments));

            using (var output = new StringWriter())
            {
                LoggingSample loggingSample = new LoggingSample(output);
                var consoleOutput = new ConsoleOutput()
                {
                    ExitCode = loggingSample.Run(arguments),
                    Stdout = output.ToString()
                };
                Console.Write(consoleOutput.Stdout);
                return consoleOutput;
            }
        }

        protected static void AssertSucceeded(ConsoleOutput output)
        {
            Assert.True(0 == output.ExitCode,
                $"Exit code: {output.ExitCode}\n{output.Stdout}");
        }

        public class LoggingTest : BaseTest, IDisposable
        {
            public void Dispose()
            {
                try
                {
                    // Delete all logs created from running the tests.
                    foreach (string log in _logsToDelete)
                    {
                        Run("delete-log", log);
                    }
                }
                catch (RpcException ex)
                {
                    // Not Found exceptions are allowed otherwise throw.
                    if (ex.Status.StatusCode != StatusCode.NotFound)
                    {
                        throw;
                    }
                }
                try
                {
                    // Delete all the log sinks created from running the tests.
                    foreach (string sink in _sinksToDelete)
                    {
                        Run("delete-sink", sink);
{'fraction_non_alphanumeric': 0.07616822429906542, 'fraction_numerical': 0.003115264797507788, 'mean_word_length': 1.5849436392914653, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5613995', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1432, 'n_words': 479}