Skip to content

Commit

Permalink
Add test repository (#190)
Browse files Browse the repository at this point in the history
* prettify on a copy of repositories

* add boolean case for primaryPrefix

* add Semicolon case for classMemberDeclaration

* fix case inferredLambdaParameter

* fix forInit case commas not printed

* fix annotationTypeBody mapVisit instead of visit

* fix classLiteralSuffix not printer Squares

* add repositories to test

* rename repository test folder

* update gitignore

* fix no output lerna run test until end exec

* add java design pattern repository to test

* fix wrong repository name

* reducing text output length

* fix cst printer shifts on unaryExpression

* removing some test repositories to reduce ci time

* fix camel case
  • Loading branch information
Shaolans authored May 9, 2019
1 parent 3555a0c commit 065b002
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 109 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ coverage
.idea
samples

packages/prettier-plugin-java/scripts/single-printer-run/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lerna:version": "lerna version",
"lerna:publish": "lerna publish from-git --yes",
"ci": "npm-run-all build lint format:validate test",
"test": "lerna run test",
"test": "lerna run test --stream",
"lint": "eslint packages/**/*.js",
"format:fix": "prettier --write \"**/*.@(js|json|md)\"",
"format:validate": "prettier --list-different \"**/*.@(js|json|md)\"",
Expand Down
3 changes: 3 additions & 0 deletions packages/prettier-plugin-java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
samples
test-samples
scripts/single-printer-run
71 changes: 5 additions & 66 deletions packages/prettier-plugin-java/scripts/clone-samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,19 @@ const sampleRepos = [
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app",
branch: "master"
}
/*{
repoUrl: "https://github.com/iluwatar/java-design-patterns.git",
branch: "1.20.0"
},
{
repoUrl: "https://github.com/spring-projects/spring-boot.git",
branch: "v2.1.0.RELEASE"
},
{
repoUrl: "https://github.com/jhipster/jhipster",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-online",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-microservice",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-gateway",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-oauth2",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-websocket",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-noi18n",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-nocache",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-hazelcast",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-elasticsearch",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-dto",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-couchbase",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-cassandra",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-mongodb",
branch: "master"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-gradle",
branch: "master"
repoUrl: "https://github.com/spring-projects/spring-boot.git",
branch: "v2.1.0.RELEASE"
},
{
repoUrl: "https://github.com/jhipster/jhipster-sample-app-react",
branch: "master"
}*/
repoUrl: "https://github.com/iluwatar/java-design-patterns.git",
branch: "1.20.0"
}
];

fs.emptyDirSync(samplesDir);
Expand Down
4 changes: 2 additions & 2 deletions packages/prettier-plugin-java/scripts/update-test-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ let originalSamplesDir = samplesDir;
if (process.argv.indexOf("-single") > -1) {
samplesDir = path.resolve(__dirname, "./single-printer-run");
} else if (process.argv.indexOf("-repository") > -1) {
const sample = path.resolve(__dirname, "../sample");
const testSamples = path.resolve(__dirname, "../test-samples");
originalSamplesDir = path.resolve(__dirname, process.argv[3]);
samplesDir = path.resolve(sample, path.basename(originalSamplesDir));
samplesDir = path.resolve(testSamples, path.basename(originalSamplesDir));
if (fs.existsSync(samplesDir)) {
fs.removeSync(samplesDir);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/prettier-plugin-java/src/printers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class ClassesPrettierVisitor {
}

classMemberDeclaration(ctx) {
if (ctx.Semicolon) {
return getImageWithComments(this.getSingle(ctx));
}
return this.visitSingle(ctx);
}

Expand Down Expand Up @@ -176,7 +179,7 @@ class ClassesPrettierVisitor {
variableDeclaratorList(ctx) {
const variableDeclarators = this.mapVisit(ctx.variableDeclarator);
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
return rejectAndJoin(commas, variableDeclarators);
return rejectAndJoinSeps(commas, variableDeclarators);
}

variableDeclarator(ctx) {
Expand Down
69 changes: 46 additions & 23 deletions packages/prettier-plugin-java/src/printers/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {
sortAnnotationIdentifier,
sortTokens,
rejectAndJoinSeps,
findDeepElementInPartsArray
findDeepElementInPartsArray,
isExplicitLambdaParameter
} = require("./printer-utils");

class ExpressionsPrettierVisitor {
Expand Down Expand Up @@ -47,7 +48,6 @@ class ExpressionsPrettierVisitor {
lambdaParametersWithBraces(ctx) {
const lambdaParameterList = this.visit(ctx.lambdaParameterList);

//console.log(JSON.stringify(lambdaParameterList)+" "+findDeepElementInPartsArray(lambdaParameterList, ','))
if (findDeepElementInPartsArray(lambdaParameterList, ",")) {
return rejectAndConcat([
ctx.LBrace[0],
Expand All @@ -58,10 +58,13 @@ class ExpressionsPrettierVisitor {

// removing braces when only no comments attached
if (
(ctx.LBrace && ctx.RBrace && !lambdaParameterList) ||
(ctx.LBrace[0].leadingComments ||
(ctx.LBrace[0].trailingComments && ctx.RBrace[0].leadingComments) ||
ctx.RBrace[0].trailingComments)
(ctx.LBrace &&
ctx.RBrace &&
(!lambdaParameterList || isExplicitLambdaParameter(ctx))) ||
ctx.LBrace[0].leadingComments ||
ctx.LBrace[0].trailingComments ||
ctx.RBrace[0].leadingComments ||
ctx.RBrace[0].trailingComments
) {
return rejectAndConcat([
ctx.LBrace[0],
Expand Down Expand Up @@ -171,18 +174,41 @@ class ExpressionsPrettierVisitor {
segment.push(rejectAndJoin(" ", [token, expression.shift()]));
} else if (
i + 1 < sortedTokens.length &&
((sortedTokens[i].image === ">" && sortedTokens[i + 1].image === ">") ||
(sortedTokens[i].image === "<" && sortedTokens[i + 1].image === "<"))
((sortedTokens[i].image === ">" &&
sortedTokens[i + 1].image === ">" &&
sortedTokens[i].startOffset ===
sortedTokens[i + 1].startOffset - 1) ||
(sortedTokens[i].image === "<" &&
sortedTokens[i + 1].image === "<" &&
sortedTokens[i].startOffset ===
sortedTokens[i + 1].startOffset - 1))
) {
// TODO: fix here by implementing print for s << 2, s >> 2 and s >>> 2
// currently work only for s << 2 and s >> 2
segment.push(
rejectAndJoin(" ", [
rejectAndConcat([token, sortedTokens[i + 1]]),
unaryExpression.shift()
])
);
i += 1;
if (
sortedTokens[i + 2] &&
sortedTokens[i + 2].image === ">" &&
sortedTokens[i + 1].startOffset ===
sortedTokens[i + 2].startOffset - 1
) {
segment.push(
rejectAndJoin(" ", [
rejectAndConcat([
token,
sortedTokens[i + 1],
sortedTokens[i + 2]
]),
unaryExpression.shift()
])
);
i++;
} else {
segment.push(
rejectAndJoin(" ", [
rejectAndConcat([token, sortedTokens[i + 1]]),
unaryExpression.shift()
])
);
}
i++;
} else if (matchCategory(token, "'BinaryOperator'")) {
segment.push(
indent(
Expand All @@ -205,7 +231,6 @@ class ExpressionsPrettierVisitor {
const unarySuffixOperator = ctx.UnarySuffixOperator
? ctx.UnarySuffixOperator
: [];

return rejectAndConcat([
rejectAndConcat(unaryPrefixOperator),
primary,
Expand Down Expand Up @@ -256,7 +281,7 @@ class ExpressionsPrettierVisitor {
}

primaryPrefix(ctx) {
if (ctx.This || ctx.Void) {
if (ctx.This || ctx.Void || ctx.Boolean) {
return getImageWithComments(this.getSingle(ctx));
}

Expand Down Expand Up @@ -486,15 +511,13 @@ class ExpressionsPrettierVisitor {
}

classLiteralSuffix(ctx) {
let squares = "";
const squares = [];
if (ctx.LSquare) {
squares = [];
for (let i = 0; i < ctx.LSquare.length; i++) {
squares.push(concat([ctx.LSquare[i], ctx.RSquare[i]]));
}
}

return rejectAndConcat([squares, concat([ctx.Dot[0], ctx.Class[0]])]);
return rejectAndConcat([...squares, ctx.Dot[0], ctx.Class[0]]);
}

arrayAccessSuffix(ctx) {
Expand Down
7 changes: 5 additions & 2 deletions packages/prettier-plugin-java/src/printers/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ class InterfacesPrettierVisitor {
}

annotationTypeBody(ctx) {
const annotationTypeMemberDeclaration = this.visit(
const annotationTypeMemberDeclaration = this.mapVisit(
ctx.annotationTypeMemberDeclaration
);

return rejectAndJoin(line, [
indent(
rejectAndJoin(line, [ctx.LCurly[0], annotationTypeMemberDeclaration])
rejectAndJoin(line, [
ctx.LCurly[0],
rejectAndJoin(concat([line, line]), annotationTypeMemberDeclaration)
])
),
ctx.RCurly[0]
]);
Expand Down
13 changes: 12 additions & 1 deletion packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ function findDeepElementInPartsArray(item, elt) {

return false;
}

function isExplicitLambdaParameter(ctx) {
return (
ctx &&
ctx.lambdaParameterList &&
ctx.lambdaParameterList[0] &&
ctx.lambdaParameterList[0].children &&
ctx.lambdaParameterList[0].children.explicitLambdaParameterList
);
}
module.exports = {
buildFqn,
rejectAndJoin,
Expand All @@ -165,5 +175,6 @@ module.exports = {
matchCategory,
sortModifiers,
rejectAndJoinSeps,
findDeepElementInPartsArray
findDeepElementInPartsArray,
isExplicitLambdaParameter
};
28 changes: 23 additions & 5 deletions packages/prettier-plugin-java/test/prettify-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
"use strict";
const { resolve } = require("path");
const { testRepositorySample } = require("./test-utils");

const jhipsterRepository = ["jhipster", "jhipster-sample-app"];

describe("prettier-java", () => {
require("./test-utils").testRepositorySample(
resolve(__dirname, "../samples/jhipster-sample-app"),
"./mvnw",
["compile"],
{ cwd: resolve(__dirname, "../samples/jhipster-sample-app") }
testRepositorySample(
resolve(__dirname, "../samples/java-design-patterns"),
"true",
[]
);

testRepositorySample(resolve(__dirname, "../samples/spring-boot"), "./mvnw", [
"clean",
"install",
"-Ddisable.checks",
"-DskipTests"
]);

jhipsterRepository.forEach(repository => {
testRepositorySample(
resolve(__dirname, `../samples/${repository}`),
"./mvnw",
["compile"]
);
});
});
29 changes: 22 additions & 7 deletions packages/prettier-plugin-java/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@

const prettier = require("prettier");
const { expect } = require("chai");
const { readFileSync, writeFileSync } = require("fs");
const { resolve, relative } = require("path");
const {
readFileSync,
writeFileSync,
existsSync,
removeSync,
copySync
} = require("fs-extra");
const { resolve, relative, basename } = require("path");
const klawSync = require("klaw-sync");
const { spawnSync } = require("child_process");

Expand Down Expand Up @@ -46,17 +52,24 @@ function testSample(testFolder, exclusive) {
});
}

function testRepositorySample(testFolder, command, args, options) {
function testRepositorySample(testFolder, command, args) {
describe(`Prettify the repository <${testFolder}>`, () => {
const sampleFiles = klawSync(resolve(__dirname, testFolder), {
const testsamples = resolve(__dirname, "../test-samples");
const samplesDir = resolve(testsamples, basename(testFolder));
if (existsSync(samplesDir)) {
removeSync(samplesDir);
}
copySync(testFolder, samplesDir);

const sampleFiles = klawSync(resolve(__dirname, samplesDir), {
nodir: true
});
const javaSampleFiles = sampleFiles.filter(fileDesc =>
fileDesc.path.endsWith(".java")
);

javaSampleFiles.forEach(fileDesc => {
it(`try to prettify ${fileDesc.path}`, () => {
it(`prettify ${relative(samplesDir, fileDesc.path)}`, () => {
const javaFileText = readFileSync(fileDesc.path, "utf8");
expect(() => {
try {
Expand All @@ -76,9 +89,11 @@ function testRepositorySample(testFolder, command, args, options) {

it(`verify semantic validity ${testFolder}`, function(done) {
this.timeout(0);
const code = spawnSync(command, args, options);
const code = spawnSync(command, args, {
cwd: samplesDir
});
if (code.status !== 0) {
console.error(code.stdout);
console.error(code.stdout.toString());
expect.fail(
`Cannot build ${testFolder}, please check the output above.`
);
Expand Down

0 comments on commit 065b002

Please sign in to comment.