<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Vault.Tests
{
    public class VaultTestServer : IDisposable
    {
        private Process _process;
        public string RootToken = Guid.NewGuid().ToString();
        public string ListenAddress = $"127.0.0.1:{ GetRandomUnusedPort() }";

        public VaultClient TestClient()
        {
            var vaultArgs = string.Join(" ", new List<string>
            {
                "server",
                "-dev",
                $"-dev-root-token-id={RootToken}",
                $"-dev-listen-address={ListenAddress}"
            });

            _process = new Process
            {
                StartInfo = new ProcessStartInfo("vault", vaultArgs),
            };
            _process.StartInfo.RedirectStandardOutput = true;
            _process.StartInfo.RedirectStandardError = true;

            Thread.Sleep(500);

            if (!_process.Start())
            {
                throw new Exception($"Process did not start successfully: {_process.StandardError}");
            }

            if (_process.HasExited)
            {
                throw new Exception($"Process could not be started: {_process.StandardError}");
            }

            return new VaultClient(new UriBuilder(ListenAddress).Uri, RootToken);
        }

        private static int GetRandomUnusedPort()<commit_msg>Adding support for passing the vault binary by environment variables<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Vault.Tests
{
    public class VaultTestServer : IDisposable
    {
        private Process _process;
        public string RootToken = Guid.NewGuid().ToString();
        public string ListenAddress = $"127.0.0.1:{ GetRandomUnusedPort() }";

        public VaultClient TestClient()
        {
            var vaultArgs = string.Join(" ", new List<string>
            {
                "server",
                "-dev",
                $"-dev-root-token-id={RootToken}",
                $"-dev-listen-address={ListenAddress}"
            });

            var vaultBin = Environment.GetEnvironmentVariable("VAULT_BIN") ?? "vault";
            _process = new Process
            {
                StartInfo = new ProcessStartInfo(vaultBin, vaultArgs),
            };
            _process.StartInfo.RedirectStandardOutput = true;
            _process.StartInfo.RedirectStandardError = true;

            Thread.Sleep(500);

            if (!_process.Start())
            {
                throw new Exception($"Process did not start successfully: {_process.StandardError}");
            }

            if (_process.HasExited)
            {
                throw new Exception($"Process could not be started: {_process.StandardError}");
            }

            return new VaultClient(new UriBuilder(ListenAddress).Uri, RootToken);
        }

        private static int GetRandomUnusedPort()
{'fraction_non_alphanumeric': 0.10029022895840052, 'fraction_numerical': 0.005804579168010319, 'mean_word_length': 1.9458689458689458, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3729037', 'n_tokens_mistral': 804, 'n_tokens_neox': 758, 'n_words': 178}