ngetchell / PSGitLab

An interface for administering GitLab from the PowerShell command line.
MIT License
72 stars 42 forks source link

Code cleanup #172

Closed it-praktyk closed 6 years ago

it-praktyk commented 6 years ago

The style rules can be checked using tests like below - but it should be made before merging to the Release Tests are from Pester.

Describe 'Style rules' -Tag StyleRules {
    $moduleRoot = "..\"

    $files = @(
        Get-ChildItem $moduleRoot\* -Include *.ps1,*.psm1, *.psd1
        Get-ChildItem (Join-Path $moduleRoot 'PSGitLab') -Include *.ps1,*.psm1, *.psd1, *.txt -Recurse
        Get-ChildItem (Join-Path $moduleRoot 'Tests') -Include *.ps1,*.psm1, *.psd1 -Recurse
    )

    It 'Pester source files contain no trailing whitespace' {
        $badLines = @(
            foreach ($file in $files)
            {
                $lines = [System.IO.File]::ReadAllLines($file.FullName)
                $lineCount = $lines.Count

                for ($i = 0; $i -lt $lineCount; $i++)
                {
                    if ($lines[$i] -match '\s+$') {
                        'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
                    }
                }
            }
        )

        if ($badLines.Count -gt 0)
        {
            throw "The following $($badLines.Count) lines contain trailing whitespace: $([System.Environment]::NewLine)$([System.Environment]::NewLine)$($badLines -join "$([System.Environment]::NewLine)")"
        }
    }
    It 'Spaces are used for indentation in all code files, not tabs' {
        $badLines = @(
            foreach ($file in $files)
            {
                $lines = [System.IO.File]::ReadAllLines($file.FullName)
                $lineCount = $lines.Count

                for ($i = 0; $i -lt $lineCount; $i++)
                {
                    if ($lines[$i] -match '^[  ]*\t|^\t|^\t[  ]*') {
                        'File: {0}, Line: {1}' -f $file.FullName, ($i + 1)
                    }
                }
            }
        )

        if ($badLines.Count -gt 0)
        {
            throw "The following $($badLines.Count) lines start with a tab character: $([System.Environment]::NewLine)$([System.Environment]::NewLine)$($badLines -join "$([System.Environment]::NewLine)")"
        }
    }

    It 'Pester Source Files all end with a newline' {
        $badFiles = @(
            foreach ($file in $files) {
                $string = [System.IO.File]::ReadAllText($file.FullName)
                if ($string.Length -gt 0 -and $string[-1] -ne "`n") {
                    $file.FullName
                }
            }
        )

        if ($badFiles.Count -gt 0) {
            throw "The following files do not end with a newline: $([System.Environment]::NewLine)$([System.Environment]::NewLine)$($badFiles -join "$([System.Environment]::NewLine)")"
        }
    }
}
ngetchell commented 6 years ago

I am happy to accept the PR up to commit ffa79be0eb86718ef3296b9f1d847b6970231505. The additional style rules enforced during the build isn't something I'd like to add.

I've gotten complaints that adding more functions to cover more of the API is overwhelming so I'm going to try to reduce the number of moving parts. I would like to reduce the barrier to entry. This would involve the following:

it-praktyk commented 6 years ago

@ngetchell, I changed - some time ago - the code as you requested. Can you merge it now?

CC: @X-Guardian, @tdemeester

ngetchell commented 6 years ago

Not sure how I missed this. Thanks for the comment.