Navigation Menu

Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
New test infrastructure (#4188)
Browse files Browse the repository at this point in the history
* New test infrastructure

* Enable push to Git

* fix reporting

* links to errors

* better visual on errors

* Improved logging

* Fixes to content

* Fixed logging when everything passes

* Moves to JS Script

* logging fix

* Don’t use test files

* Adds checks for files changed

* Don't write extra file

* Remove checkIfUpdatedUpdated
  • Loading branch information
petele committed Feb 15, 2017
1 parent b189182 commit 2de7f06
Show file tree
Hide file tree
Showing 80 changed files with 2,182 additions and 685 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -9,8 +9,7 @@ tools/claat
gcloud-client-secret.json

## Files that are autogenerated by the build system
build/*
_build-me.yaml
test-results.json
src/content/**/*/_files.json
src/content/**/*/atom.xml
src/content/**/*/rss.xml
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -17,6 +17,7 @@ install:
script:
- npm run build
- npm run test
- node ./tools/travis/addCommitComment.js
after_success:
- ./tools/travis/install-gc-sdk.sh
- ./tools/travis/deploy-master.sh
Expand Down
4 changes: 4 additions & 0 deletions app.yaml
Expand Up @@ -5,6 +5,10 @@ threadsafe: true
# version: wf-devsite

handlers:
- url: /test-results.json
static_files: test-results.json
upload: test-results.json

- url: /robots.txt
static_files: gae/robots.txt
upload: gae/robots.txt
Expand Down
74 changes: 74 additions & 0 deletions gulp-tasks/remark-lint-tests/check-headings.js
@@ -0,0 +1,74 @@
'use strict';

const wfRegEx = require('../wfRegEx');
const visit = require('unist-util-visit');
var toString = require('mdast-util-to-string');

module.exports = {
'wf-headings-tldr': wfTLDR,
'wf-headings-blank': wfHeadingsBlank,
'wf-headings-in-markdown': wfHeadingsInMarkdown,
'wf-headings-no-markup-in-title': wfNoMarkupInTitle,
};

const reEntity = /&\w*?;/;
const reHTML = /</;
const reMD = /`/;
const reTLDR = /tl;dr/i;
const reHeading = /^<h\d>.*?<\/h\d>$/i;
const reHideFromTOC = /.hide-from-toc/;

function wfHeadingsInMarkdown(ast, file, setting) {
let msg = 'Headings must use markdown style, HTML is not permitted.';
visit(ast, 'html', function (node) {
if (reHeading.test(node.value)) {
file.message(msg, node);
}
});
}

function wfHeadingsBlank(ast, file, setting) {
visit(ast, 'heading', function(node) {
let title = toString(node).trim();
if (title.length === 0) {
file.message('Headings cannot be empty.', node);
}
});
}

function wfNoMarkupInTitle(ast, file, setting) {
visit(ast, 'heading', function(node) {
if (node.depth !== 1) {
return;
}
let title = toString(node);
if (reHTML.test(title)) {
file.message('Top level headings cannot contain HTML elements', node);
}
if (reMD.test(title)) {
file.message('Top level headings cannot contain Markdown', node);
}
if (reEntity.test(title)) {
file.message('Top level headings cannot contain encoded entities', node);
}
});
}

function wfTLDR(ast, file, setting) {
let msgLevel = 'TL;DR headings must be level 3 or greater.';
let msgHide = 'TL;DR headings must be hidden from the TOC with `{: .hide-from-toc }`';
visit(ast, 'heading', function(node) {
let body = toString(node);
if (reTLDR.test(body)) {
if (node.depth < 3) {
file.message(msgLevel, node);
}
if (!reHideFromTOC.test(body)) {
file.message(msgHide, node);
}
}
});
}



73 changes: 73 additions & 0 deletions gulp-tasks/remark-lint-tests/check-html.js
@@ -0,0 +1,73 @@
'use strict';

const url = require('url');
const wfRegEx = require('../wfRegEx');
const visit = require('unist-util-visit');
const toString = require('mdast-util-to-string');

module.exports = {
'wf-html-you-tube': wfYouTube,
'wf-html-dgc-links': wfHTMLDGCLinks,
'wf-html-internal-links': wfInternalLinks,
'wf-html-link-forced-lang': wfForcedLang,
'wf-html-unsafe-short-links': wfUnsafeShortLinks
};

const reYouTube = /^<iframe\s.*?src=['|"]https?:\/\/(www.)?youtube.com\/.*?['|"| ].*>[.\s\n\r]*?<\/iframe>$/i;
const reDGCLink = /^<a\s.*?href=['|"]?(https?:)?\/\/developers.google.com/i;
const reSandboxedLink = /^<a\s.*?href=['|"]?(https?:)?\/\/sandbox.google.com/i;
const reGooGL = /^<a\s.*?href=['|"]?http:\/\/goo\.gl\//i;
const reHref = /^<a\s.*href=['|"](.*)['|"]>/i;

function wfYouTube(ast, file, setting) {
let msg = 'YouTube videos must use DevSite embed.';
visit(ast, 'html', function (node) {
if (reYouTube.test(node.value)) {
file.message(msg, node);
}
});
}

function wfHTMLDGCLinks(ast, file, setting) {
let msg = 'Do not hard code `developers.google.com` in links.';
visit(ast, 'html', function (node) {
if (reDGCLink.test(node.value)) {
file.message(msg, node);
}
});
}

function wfInternalLinks(ast, file, setting) {
let msg = 'Do not use internal Google sandboxed links';
visit(ast, 'html', function (node) {
if (reSandboxedLink.test(node.value)) {
file.message(msg, node);
}
});
}

function wfUnsafeShortLinks(ast, file, setting) {
let msg = 'Do not use unsafe `HTTP://goo.gl/` links';
visit(ast, 'html', function (node) {
if (reGooGL.test(node.value)) {
file.message(msg, node);
}
});
}

function wfForcedLang(ast, file, setting) {
let msg = 'Hard coded language URL in link (`hl=xx`).';
visit(ast, 'html', function (node) {
let match = reHref.exec(node.value);
if (match && match[1]) {
let parsedUrl = url.parse(match[1]);
let queryString = parsedUrl.query;
if (queryString && queryString.toLowerCase().indexOf('hl=') >= 0) {
file.message(msg, node);
}
}
});
}



57 changes: 57 additions & 0 deletions gulp-tasks/remark-lint-tests/check-images.js
@@ -0,0 +1,57 @@
'use strict';

const fs = require('fs');
const path = require('path');
const visit = require('unist-util-visit');

module.exports = {
'wf-images-md': wfImagesInMD,
'wf-images-html': wfImagesInHTML,
};

const reRemote = /(https?:)?\/\//i;
const reImgTag = /<img /i;
const reImgSrc = /src=['|"](.*?)['|"|>|\s]/i;


function doesImageExist(file, imgPath, node) {
let msgHardCoded = 'Do not hard code `developers.google.com` in paths.';
if (reRemote.test(imgPath)) {
if (imgPath.indexOf('developers.google.com') > 0) {
file.message(msgHardCoded, node);
imgPath = imgPath.replace(/https?:\/\/developers\.google\.com/i, '');
} else {
return;
}
}
let filePath = file.cwd;
if (imgPath.indexOf('/') === 0) {
filePath = path.join(filePath, 'src/content/en', imgPath.replace('/web/', ''));
} else {
filePath = path.join(filePath, file.dirname, imgPath);
}
try {
fs.accessSync(filePath, fs.R_OK);
return true;
} catch (ex) {
let msg = `Unable to find image \`${imgPath}\``;
file.message(msg, node);
}
}

function wfImagesInMD(ast, file, setting) {
visit(ast, 'image', function (node) {
if (node.url && node.url.trim().length > 0) {
doesImageExist(file, node.url, node);
}
});
}

function wfImagesInHTML(ast, file, setting) {
visit(ast, 'html', function (node) {
let matched = reImgSrc.exec(node.value);
if (matched && matched[1]) {
doesImageExist(file, matched[1], node);
}
});
}
62 changes: 62 additions & 0 deletions gulp-tasks/remark-lint-tests/check-links.js
@@ -0,0 +1,62 @@
'use strict';

const url = require('url');
const visit = require('unist-util-visit');

module.exports = {
'wf-links-forced-lang': wfForcedLang,
'wf-links-dgc': wfDGCLinks,
'wf-links-unsafe-short': wfUnsafeShort,
'wf-links-internal': wfInternalLinks
};

// Check for links with hard coded languages
function wfForcedLang(ast, file, setting) {
let msg = 'Hard coded language URL in link (`hl=xx`)';
visit(ast, 'link', function (node) {
let parsedUrl = url.parse(node.url);
let queryString = parsedUrl.query;
if (queryString && queryString.toLowerCase().indexOf('hl=') >= 0) {
file.message(msg, node);
}
});
}

// Check for links with FQDN to DevSite
function wfDGCLinks(ast, file, setting) {
let msg = 'Do not hard code `developers.google.com` in links.';
visit(ast, 'link', function (node) {
let parsedUrl = url.parse(node.url);
let hostname = parsedUrl.hostname;
if (hostname && hostname.toLowerCase() === 'developers.google.com') {
file.message(msg, node);
}
});
}

// Check for unsecured shortlinks
function wfUnsafeShort(ast, file, setting) {
let msg = 'Do not use unsafe `HTTP://goo.gl/` links';
visit(ast, 'link', function (node) {
let parsedUrl = url.parse(node.url);
let protocol = parsedUrl.protocol;
let hostname = parsedUrl.hostname;
if (hostname && hostname.toLowerCase() === 'goo.gl') {
if (protocol && protocol.toLowerCase() === 'http:') {
file.message(msg, node);
}
}
});
}

// Check for internal & sandboxed links
function wfInternalLinks(ast, file, setting) {
let msg = 'Do not use internal Google sandboxed links.';
visit(ast, 'link', function (node) {
let parsedUrl = url.parse(node.url);
let hostname = parsedUrl.hostname;
if (hostname && hostname.toLowerCase() === 'sandbox.google.com') {
file.message(msg, node);
}
});
}

0 comments on commit 2de7f06

Please sign in to comment.