Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require directoryOrFile argument when not piping stdin #388

Merged
merged 7 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/validatePR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: dotnet test Src/CSharpier.Tests/CSharpier.Tests.csproj -c release
test_stdin:
test_cli:
runs-on: ubuntu-latest
shocklateboy92 marked this conversation as resolved.
Show resolved Hide resolved
name: Test StdIn
name: Test Cli
steps:
- uses: actions/checkout@v2
- run: |
dotnet build Src/CSharpier/CSharpier.csproj -c release
echo "public class ClassName { }" > code.cs
cat code.cs | dotnet Src/CSharpier/bin/release/net5.0/dotnet-csharpier.dll | diff - code.cs
pwsh ./Scripts/TestCli.ps1
check_formatting:
runs-on: ubuntu-latest
name: Check Formatting
steps:
- uses: actions/checkout@v2
- run: |
dotnet tool restore
dotnet csharpier --check
dotnet csharpier . --check
2 changes: 2 additions & 0 deletions CSharpier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ ProjectSection(SolutionItems) = preProject
Scripts/ChangeLog.ps1 = Scripts/ChangeLog.ps1
Scripts/UpdateForkedRepos.ps1 = Scripts/UpdateForkedRepos.ps1
Scripts\CreateTestingPR.ps1 = Scripts\CreateTestingPR.ps1
Scripts\TestCli.ps1 = Scripts\TestCli.ps1
Scripts\Helpers.ps1 = Scripts\Helpers.ps1
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{7C99AEA7-E608-40B5-9B2E-353B6E4C9DBE}"
Expand Down
4 changes: 3 additions & 1 deletion Docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ Options:
```

### \[<directoryOrFile\>]
If this is not supplied, then CSharpier will recursively format all files it finds starting from the current directory.

If a list of paths is supplied
- if the path points to an existing file, CSharpier will format that file
- if the path points to an existing directory, CSharpier will recursively format the contents of that directory

If a list of paths is not supplied, then stdin is read.

### --check
Used to check if your files are already formatted. Outputs any files that have not already been formatted.
This will return exit code 1 if there are unformatted files which is useful for CI pipelines.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dotnet tool install -g csharpier
### Basic Usage
Run csharpier from the directory you wish to format.
```console
dotnet csharpier
dotnet csharpier .
```

### MsBuild Package
Expand Down
2 changes: 1 addition & 1 deletion Scripts/CreateReviewCodePRs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ foreach($folder in Get-ChildItem $csharpierRepos) {

Push-Location $csharpierRepos

dotnet $csharpierProject\Src\CSharpier\bin\Release\net5.0\dotnet-csharpier.dll
dotnet $csharpierProject\Src\CSharpier\bin\Release\net5.0\dotnet-csharpier.dll .
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2 comments I made about CreateTestingPr.ps1 apply to this file too.


foreach($folder in Get-ChildItem $csharpierRepos)
{
Expand Down
36 changes: 23 additions & 13 deletions Scripts/CreateTestingPR.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
param (
[string]$pathToTestingRepo
)

if (!$pathToTestingRepo) {
$pathToTestingRepo = "C:\Projects\csharpierForkedRepos\aspnetcore"
}
if (!(Test-Path $pathToTestingRepo)) {
Write-Output "No directory found at $($pathToTestingRepo)."
Write-Output "Please checkout out https://github.com/belav/aspnetcore there or supply -pathToTestingRepo"
exit 1
}

. $PsScriptRoot/Helpers.ps1

$ErrorActionPreference = "Stop"

$branch = & git branch --show-current
Expand All @@ -10,11 +25,6 @@ if ($branch -eq "master") {
$preBranch = "pre-" + $branch
$postBranch = "post-" + $branch

$csharpierProject = (Get-Item $PSScriptRoot).Parent.FullName

# TODO this should probably be a parameter
$testingRepo = "C:\Projects\csharpierForkedRepos\aspnetcore"

Set-Location $testingRepo
& git reset --hard

Expand All @@ -23,37 +33,37 @@ $postBranchOutput = (git status) | Out-String
$firstRun = -not $postBranchOutput.Contains("On branch $postBranch")
if ($firstRun)
{
Set-Location $csharpierProject
Set-Location $repositoryRoot
# TODO this should make sure the working tree is clean
& git checkout master
& dotnet build Src\CSharpier\CSharpier.csproj -c Release
Build-CSharpier

Set-Location $testingRepo
Set-Location $pathToTestingRepo

& git checkout main
& git reset --hard
& git checkout -b $preBranch

dotnet $csharpierProject\Src\CSharpier\bin\Release\net5.0\dotnet-csharpier.dll
dotnet $csharpierDllPath .

& git add -A
& git commit -m "Before $branch"
& git push --set-upstream origin $preBranch
}

Set-Location $csharpierProject
Set-Location $repositoryRoot
git checkout $branch
& dotnet build Src\CSharpier\CSharpier.csproj -c Release
Build-CSharpier

Set-Location $testingRepo
Set-Location $pathToTestingRepo

if ($firstRun) {
& git checkout -b $postBranch
} else {
& git checkout $postBranch
}

dotnet $csharpierProject\Src\CSharpier\bin\Release\net5.0\dotnet-csharpier.dll
dotnet $csharpierDllPath .

& git add -A
& git commit -m "After $branch"
Expand Down
8 changes: 8 additions & 0 deletions Scripts/Helpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$repositoryRoot = Join-Path $PSScriptRoot ".."
$csProjectPath = Join-Path $repositoryRoot "Src/CSharpier/CSharpier.csproj"
$csharpierDllPath = Join-Path $repositoryRoot "Src/CSharpier/bin/release/net5.0/dotnet-csharpier.dll"

function Build-CSharpier
{
& dotnet build $csProjectPath -c release
}
90 changes: 90 additions & 0 deletions Scripts/TestCli.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
$ErrorActionPreference = "Stop"

. $PsScriptRoot/Helpers.ps1

if (-not (Test-Path $csharpierDllPath)) {
Write-Output "No dll found at $($csharpierDllPath)"
exit 1
}

if (-not (Test-Path "TestCli"))
{
New-Item "TestCli" -ItemType "Directory"
}

Set-Location "TestCli"

$formatted = "public class ClassName { }"
$unformatted = "public class ClassName { }"

$failed = $false;

Write-Output "---- File provided by stdIn should be formatted"
$stdInResult = $unformatted | dotnet $csharpierDllPath
if ($stdInResult -ne $formatted) {
Write-Output "Piping c# code to CSharpier did not result in formatted code. The result was:"
Write-Output $stdInResult
Write-Output ""
$failed = $true;
}

Write-Output "---- Check should exit with 1 when file not formatted"
New-Item -Path . -Name "Check.cs" -Value $unformatted 2>&1 | Out-Null
$checkResult = dotnet $csharpierDllPath Check.cs --check
$checkResult = $checkResult -join "`n"
if ($LASTEXITCODE -ne 1) {
Write-Output "The exit code from --check was $($LASTEXITCODE) but was expected to be 1"
Write-Output ""
$failed = $true
}
Remove-Item "Check.cs"

Write-Output "---- Check should print unformatted file"
$wasNotFormatted = "was not formatted"
if (-not ($checkResult.Contains($wasNotFormatted))) {
Write-Output "The result from --check did not contain '$($wasNotFormatted)', it was: "
Write-Output $checkResult
Write-Output ""
$failed = $true
}

Write-Output "---- Basic file should be formatted"
New-Item -Path . -Name "Code.cs" -Value $unformatted 2>&1 | Out-Null
dotnet $csharpierDllPath Code.cs 2>&1 | Out-Null
$codeContents = Get-Content "Code.cs"
if ($codeContents -ne $formatted) {
Write-Output "The result of formatting a basic class was"
Write-Output $codeContents
Write-Output "Expected"
Write-Output $formatted
Write-Output ""
}
Remove-Item "Code.cs"

Write-Output "---- Ignore file respected when directoryOrFile is '.' and csharpierignore has subdirectory"
New-Item -Path "Subdirectory" -ItemType "Directory" | Out-Null
New-Item -Path "Subdirectory" -Name "IgnoredFile.cs" -Value $unformatted | Out-Null
New-Item -Path . -Name ".csharpierignore" -Value "Subdirectory/IgnoredFile.cs" | Out-Null
dotnet $csharpierDllPath . 2>&1 | Out-Null
$ignoredFileContents = Get-Content "Subdirectory/IgnoredFile.cs"
if ($ignoredFileContents -ne $unformatted) {
Write-Output "The file at Subdirectory/IgnoredFile.cs should have been ignored but it was formatted"
Write-Output ""
}
Remove-Item "Subdirectory" -Recurse -Force
Remove-Item ".csharpierignore" -Force

Write-Output "---- DirectoryOrFile is required when not using stdin"
$noDirectoryResult = dotnet $csharpierDllPath 2>&1 | Out-Null
$noDirectoryResult = $noDirectoryResult -join "`n"
$missingDirectoryOrFailes = "directoryOrFile is required when not piping stdin to CSharpier"
if (-not ($missingDirectoryOrFailes.Contains($missingDirectoryOrFailes))) {
Write-Output "The result from running with no options did not contain '$($missingDirectoryOrFailes)', it was: "
Write-Output $missingDirectoryOrFailes
Write-Output ""
$failed = $true
}

if ($failed) {
exit 1;
}
2 changes: 1 addition & 1 deletion Scripts/UpdateForkedRepos.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ foreach($folder in Get-ChildItem $csharpierRepos) {
& git checkout main
& git reset --hard

dotnet csharpier
dotnet csharpier .

& git add -A
& git commit -m $versionWithQuotes
Expand Down
12 changes: 11 additions & 1 deletion Src/CSharpier/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ public static RootCommand Create()
)
};

rootCommand.Description = "csharpier";
rootCommand.AddValidator(
cmd =>
{
if (!Console.IsInputRedirected && cmd.Children["directoryOrFile"] == null)
{
return "directoryOrFile is required when not piping stdin to CSharpier";
}

return null;
}
);

return rootCommand;
}
Expand Down
16 changes: 7 additions & 9 deletions Src/CSharpier/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CSharpier.SyntaxPrinter.SyntaxNodePrinters;

namespace CSharpier
{
Expand All @@ -26,7 +23,7 @@ static async Task<int> Main(string[] args)
}

public static async Task<int> Run(
string[] directoryOrFile,
string[]? directoryOrFile,
bool check,
bool fast,
bool skipWrite,
Expand All @@ -43,23 +40,24 @@ CancellationToken cancellationToken
Console.InputEncoding
);
standardInFileContents = await streamReader.ReadToEndAsync();
}

if (directoryOrFileNotProvided)
{
directoryOrFile = new[] { Directory.GetCurrentDirectory() };
}
else
{
directoryOrFile = directoryOrFile!.Select(
o => Path.Combine(Directory.GetCurrentDirectory(), o)
o =>
o == "."
// .csharpierignore gets confused by . so just don't include it
? Directory.GetCurrentDirectory()
: Path.Combine(Directory.GetCurrentDirectory(), o)
)
.ToArray();
}

var commandLineOptions = new CommandLineOptions
{
DirectoryOrFilePaths = directoryOrFile.ToArray(),
DirectoryOrFilePaths = directoryOrFile!.ToArray(),
StandardInFileContents = standardInFileContents,
Check = check,
Fast = fast,
Expand Down