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

Code before:
    {
        var dir = _fixture.CreateFileBasedApp(null);
        var secretManager = CreateProgram();

        secretManager.RunInternal("init", "--file", Path.Combine(dir, "app.cs"));
        Assert.Contains(Resources.Error_InitNotSupportedForFileBasedApps, _console.GetOutput());
    }

    [Fact]
    public void SupportsRelativePaths()
    {
        var projectPath = _fixture.GetTempSecretProject();
        var cwd = Path.Combine(projectPath, "nested1");
        Directory.CreateDirectory(cwd);
        var secretManager = new Program(_console, cwd);

        secretManager.RunInternal("list", "-p", ".." + Path.DirectorySeparatorChar, "--verbose");

        Assert.Contains(Resources.FormatMessage_Project_File_Path(Path.Combine(cwd, "..", "TestProject.csproj")), _console.GetOutput());
    }

    [Theory]
    [InlineData(false, true)]
    [InlineData(false, false)]
    [InlineData(true, false)]
    public void SetSecrets(bool fromCurrentDirectory, bool fileBasedApp)
    {
        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 ?

Review comment (security):
Should we error if you try to set secrets on a file-based app from a directory that the file is not in? Based on the options here, it seems like calling the below would be invalid?

```
$ pwd
/user/some/dir
$ dotnet user-secrets set "foo" "bar" --file /user/some/otherdir/file.cs
```

Code after:
    {
        var dir = _fixture.CreateFileBasedApp(null);
        var secretManager = CreateProgram();

        secretManager.RunInternal("init", "--file", Path.Combine(dir, "app.cs"));
        Assert.Contains(Resources.Error_InitNotSupportedForFileBasedApps, _console.GetOutput());
    }

    [Fact]
    public void SupportsRelativePaths()
    {
        var projectPath = _fixture.GetTempSecretProject();
        var cwd = Path.Combine(projectPath, "nested1");
        Directory.CreateDirectory(cwd);
        var secretManager = new Program(_console, cwd);

        secretManager.RunInternal("list", "-p", ".." + Path.DirectorySeparatorChar, "--verbose");

        Assert.Contains(Resources.FormatMessage_Project_File_Path(Path.Combine(cwd, "..", "TestProject.csproj")), _console.GetOutput());
    }

    [Theory]
    [InlineData(false, true)]
    [InlineData(false, false)]
    [InlineData(true, false)]
    public void SetSecrets(bool fromCurrentDirectory, bool fileBasedApp)
    {
        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)