Repository: dotnet/aspnetcore
File: src/Tools/dotnet-user-secrets/test/SecretManagerTests.cs (C#)

Code before:
        var secrets = new KeyValuePair<string, string>[]
                    {
                        new KeyValuePair<string, string>("key1", Guid.NewGuid().ToString()),
                        new KeyValuePair<string, string>("Facebook:AppId", Guid.NewGuid().ToString()),
                        new KeyValuePair<string, string>(@"key-@\/.~123!#$%^&*())-+==", @"key-@\/.~123!#$%^&*())-+=="),
                        new KeyValuePair<string, string>("key2", string.Empty),
                        new KeyValuePair<string, string>("-oneDashedKey", "-oneDashedValue"),
                        new KeyValuePair<string, string>("--twoDashedKey", "--twoDashedValue")
                    };

        var projectPath = fileBasedApp
            ? _fixture.GetTempFileBasedApp(out _)
            : _fixture.GetTempSecretProject();
        var dir = fromCurrentDirectory
            ? projectPath
            : Path.GetTempPath();
        ReadOnlySpan<string> pathArgs = fileBasedApp
            ? ["-f", Path.Join(projectPath, "app.cs")]
            : ["-p", projectPath];
        var secretManager = new Program(_console, dir);

        foreach (var secret in secrets)
        {
            var parameters = fromCurrentDirectory ?
                new string[] { "set", secret.Key, secret.Value, "--verbose" } :
                [ "set", secret.Key, secret.Value, .. pathArgs, "--verbose" ];
            secretManager.RunInternal(parameters);
        }

        foreach (var keyValue in secrets)
        {
            Assert.Contains(
                string.Format(CultureInfo.InvariantCulture, "Successfully saved {0} to the secret store.", keyValue.Key),
                _console.GetOutput());
        }

        _console.ClearOutput();
        var args = fromCurrentDirectory
            ? new string[] { "list", "--verbose" }
            : ["list", .. pathArgs, "--verbose"];
        secretManager.RunInternal(args);
        foreach (var keyValue in secrets)
        {
            Assert.Contains(
                string.Format(CultureInfo.InvariantCulture, "{0} = {1}", keyValue.Key, keyValue.Value),
                _console.GetOutput());
        }

        // Remove secrets.
        _console.ClearOutput();
        foreach (var secret in secrets)

Review comment (refactor):
nit: wonder if it would be simplifying if `pathArgs` was always included, and in the case `fromCurrentDirectory` is true, we would just ensure `pathArgs` is empty.


Code after:
        var secrets = new KeyValuePair<string, string>[]
                    {
                        new KeyValuePair<string, string>("key1", Guid.NewGuid().ToString()),
                        new KeyValuePair<string, string>("Facebook:AppId", Guid.NewGuid().ToString()),
                        new KeyValuePair<string, string>(@"key-@\/.~123!#$%^&*())-+==", @"key-@\/.~123!#$%^&*())-+=="),
                        new KeyValuePair<string, string>("key2", string.Empty),
                        new KeyValuePair<string, string>("-oneDashedKey", "-oneDashedValue"),
                        new KeyValuePair<string, string>("--twoDashedKey", "--twoDashedValue")
                    };

        var projectPath = fileBasedApp
            ? _fixture.GetTempFileBasedApp(out _)
            : _fixture.GetTempSecretProject();
        var dir = fromCurrentDirectory
            ? projectPath
            : Path.GetTempPath();
        ReadOnlySpan<string> pathArgs = fromCurrentDirectory
            ? []
            : (fileBasedApp
                ? ["-f", Path.Join(projectPath, "app.cs")]
                : ["-p", projectPath]);
        var secretManager = new Program(_console, dir);

        foreach (var secret in secrets)
        {
            secretManager.RunInternal(["set", secret.Key, secret.Value, .. pathArgs, "--verbose"]);
        }

        foreach (var keyValue in secrets)
        {
            Assert.Contains(
                string.Format(CultureInfo.InvariantCulture, "Successfully saved {0} to the secret store.", keyValue.Key),
                _console.GetOutput());
        }

        _console.ClearOutput();
        secretManager.RunInternal(["list", .. pathArgs, "--verbose"]);
        foreach (var keyValue in secrets)
        {
            Assert.Contains(
                string.Format(CultureInfo.InvariantCulture, "{0} = {1}", keyValue.Key, keyValue.Value),
                _console.GetOutput());
        }

        // Remove secrets.
        _console.ClearOutput();
        foreach (var secret in secrets)
        {
            secretManager.RunInternal(["remove", secret.Key, .. pathArgs, "--verbose"]);
        }