Skip to content

Commit

Permalink
Revert "Remove markdownlint tests due to security issues (PowerShel…
Browse files Browse the repository at this point in the history
…l#10163)"

This reverts commit 5ef258e.
  • Loading branch information
xtqqczze committed May 24, 2020
1 parent 1f252f8 commit e90b3a7
Show file tree
Hide file tree
Showing 4 changed files with 2,776 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/common/markdown/gulpfile.js
@@ -0,0 +1,60 @@
function RunTest() {
"use strict";
var gulp = require("gulp");
var concat = require("gulp-concat");
var through2 = require("through2");
var markdownlint = require("markdownlint");

gulp.task("test-mdsyntax", function task() {
var paths = [];
var rootpath;

// assign --repoRoot <rootpath> into rootpath
var j = process.argv.indexOf("--rootpath");
if (j > -1) {
rootpath = process.argv[j + 1];
}

if(rootpath == null)
{
throw "--rootpath <repoRoot> must be specified before all other parameters"
}

// parse --filter into paths. --rootpath must be specified first.
var j = process.argv.indexOf("--filter");
if (j > -1) {
var filters = process.argv[j + 1].split(",");
filters.forEach(function (filter) {
paths.push(rootpath + "/" + filter);
}, this);
}

if(paths.length == 0)
{
throw "--filter <filter relative to repoRoot> must be specified"
}

var rootJsonFile = rootpath + "/.markdownlint.json"
var fs = require('fs');
fs.appendFileSync('markdownissues.txt', '--EMPTY--\r\n');
return gulp.src(paths, { "read": false })
.pipe(through2.obj(function obj(file, enc, next) {
markdownlint(
{
"files": [file.path],
"config": require(rootJsonFile)
},
function callback(err, result) {
var resultString = (result || "").toString();
if (resultString) {
file.contents = Buffer.from(resultString);
}
next(err, file);
});
}))
.pipe(concat("markdownissues.txt", { newLine: "\r\n" }))
.pipe(gulp.dest("."));
});
}

RunTest();
98 changes: 98 additions & 0 deletions test/common/markdown/markdown.tests.ps1
@@ -0,0 +1,98 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Import-Module HelpersCommon
$moduleRootFilePath = Split-Path -Path $PSScriptRoot -Parent

# Identify the repository root path of the resource module
$repoRootPath = (Resolve-Path -LiteralPath (Join-path $moduleRootFilePath "../..")).ProviderPath
$repoRootPathFound = $false

Describe 'Common Tests - Validate Markdown Files' -Tag 'CI' {
BeforeAll {
Push-Location $psscriptroot
$skip = $false
$NpmInstalled = "not installed"
if (Get-Command -Name 'yarn' -ErrorAction SilentlyContinue)
{
$NpmInstalled = "Installed"
Write-Verbose -Message "Checking if Gulp is installed. This may take a few moments." -Verbose
start-nativeExecution { yarn }
if(!(Get-Command -Name 'gulp' -ErrorAction SilentlyContinue))
{
start-nativeExecution {
sudo yarn global add 'gulp@4.0.2'
}
}
if(!(Get-Command -Name 'node' -ErrorAction SilentlyContinue))
{
throw "node not found"
}
}
if(!(Get-Command -Name 'node' -ErrorAction SilentlyContinue))
{
<#
On Windows, pre-requisites are missing
For now we will skip, and write a warning. Work to resolve this is tracked in:
https://github.com/PowerShell/PowerShell/issues/3429
#>
Write-Warning "Node and yarn are required to run this test"
$skip = $true
}

$mdIssuesPath = Join-Path -Path $PSScriptRoot -ChildPath "markdownissues.txt"
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue
}

AfterAll {
Pop-Location
}

It "Should not have errors in any markdown files" -skip:$skip {
$NpmInstalled | should BeExactly "Installed"
$mdErrors = 0
Push-Location -Path $PSScriptRoot
try
{
$docsToTest = @(
'./.github/*.md'
'./README.md'
'./demos/python/*.md'
'./docker/*.md'
'./docs/building/*.md'
'./docs/community/*.md'
'./docs/host-powershell/*.md'
'./docs/cmdlet-example/*.md'
'./docs/maintainers/*.md'
'./test/powershell/README.md'
'./tools/*.md'
'./.github/ISSUE_TEMPLATE/*.md'
)
$filter = ($docsToTest -join ',')

# Gulp 4 beta is returning non-zero exit code even when there is not an error
Start-NativeExecution {
&"gulp" test-mdsyntax --silent `
--rootpath $repoRootPath `
--filter $filter
} -VerboseOutputOnError -IgnoreExitcode

}
finally
{
Pop-Location
}

$mdIssuesPath | Should -Exist

[string[]] $markdownErrors = Get-Content -Path $mdIssuesPath
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue

if ($markdownErrors -ne "--EMPTY--")
{
$markdownErrors += ' (See https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md for an explanation of the error codes)'
}

$markdownErrors -join "`n" | Should -BeExactly "--EMPTY--"
}
}
26 changes: 26 additions & 0 deletions test/common/markdown/package.json
@@ -0,0 +1,26 @@
{
"name": "powershell.common.markdown.tests",
"private": true,
"version": "1.0.0",
"description": "The PowerShell Common Markdown Tests.",
"main": "gulpfile.js",
"dependencies": {
"gulp": "^4.0.2",
"markdownlint": "^0.12.0",
"through2": "^3.0.1"
},
"devDependencies": {
"gulp-concat": "^2.6.1",
"gulp-debug": "^4.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PowerShell/PowerShell.git"
},
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/PowerShell/PowerShell/issues"
},
"homepage": "https://github.com/PowerShell/PowerShell#readme"
}

0 comments on commit e90b3a7

Please sign in to comment.