// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.IO; namespace Microsoft.Extensions.Configuration.UserSecrets.Tests { public class UserSecretsTestFixture : IDisposable { private Stack _disposables = new Stack(); public const string TestSecretsId = "b918174fa80346bbb7f4a386729c0eff"; public UserSecretsTestFixture() { _disposables.Push(() => TryDelete(Path.GetDirectoryName(PathHelper.GetSecretsPathFromSecretsId(TestSecretsId)))); } public void Dispose() { while (_disposables.Count > 0) { _disposables.Pop()?.Invoke(); } } public string GetTempSecretProject() { string userSecretsId; return GetTempSecretProject(out userSecretsId); } private const string ProjectTemplate = @" Exe netcoreapp2.1 {0} false "; public string GetTempSecretProject(out string userSecretsId) { userSecretsId = Guid.NewGuid().ToString(); return CreateProject(userSecretsId); } public string CreateProject(string userSecretsId) { var projectPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "usersecretstest", Guid.NewGuid().ToString())); var prop = string.IsNullOrEmpty(userSecretsId) ? string.Empty : $"{userSecretsId}"; File.WriteAllText( Path.Combine(projectPath.FullName, "TestProject.csproj"), string.Format(ProjectTemplate, prop)); var id = userSecretsId; _disposables.Push(() => { try { // may throw if id is bad var secretsDir = Path.GetDirectoryName(PathHelper.GetSecretsPathFromSecretsId(id)); TryDelete(secretsDir); } catch { } }); _disposables.Push(() => TryDelete(projectPath.FullName)); return projectPath.FullName; } private static void TryDelete(string directory) { try { if (Directory.Exists(directory)) { Directory.Delete(directory, true); } } catch (Exception) { // Ignore failures. Console.WriteLine("Failed to delete " + directory); } } } }