Skip to content

Commit

Permalink
Merge branch 'release/2.0.0-alpha.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Apr 16, 2024
2 parents 9a06b42 + 903e702 commit 019c35a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: tests

on:
push:
branches:
- master
- develop
pull_request:

jobs:
unit:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install modules
run: npm install
- name: Run tests
run: npm run test -- --coverage
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

## v2.0.0-alpha.1 - 2024.04.16

### Added

- Add support for both `-c` or `-l` flag from the Prettier command ([741f555](https://github.com/studiometa/prettier-formatter-gitlab/commit/741f555))

## v2.0.0-alpha.0 - 2024.04.15

### Changed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@studiometa/prettier-formatter-gitlab",
"version": "2.0.0-alpha.0",
"version": "2.0.0-alpha.1",
"description": "A Prettier formatter for the GitLab Code Quality report",
"main": "src/index.js",
"type": "module",
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {
CI_CONFIG_PATH = '.gitlab-ci.yml',
CI_JOB_NAME,
CI_PROJECT_DIR = cwd(),
PRETTIER_CODE_QUALITY_REPORT,
} = env;

/**
Expand Down Expand Up @@ -39,9 +38,14 @@ function getOutputPath() {
*/
function parse(results) {
return results
.toString()
.split('\n')
.filter((line) => line);
.filter(
(line) =>
Boolean(line) &&
!line.startsWith('Checking formatting...') &&
!line.includes('Code style issues found'),
)
.map((line) => line.replace('[warn] ', ''));
}

/**
Expand All @@ -50,6 +54,7 @@ function parse(results) {
* @returns {Promise<void>}
*/
export async function prettierFormatterGitLab(results) {
const { PRETTIER_CODE_QUALITY_REPORT } = env;
if (CI_JOB_NAME || PRETTIER_CODE_QUALITY_REPORT) {
const files = parse(results);

Expand Down
27 changes: 26 additions & 1 deletion test/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect, afterEach } from 'bun:test';
import { describe, it, expect, afterEach, spyOn } from 'bun:test';
import { existsSync, unlinkSync, readFileSync } from 'node:fs';
import { execSync } from 'node:child_process';
import { prettierFormatterGitLab } from '../src/index.js';

const CODE_QUALITY_FILENAME = 'gl-prettier-codequality.json';

Expand All @@ -24,4 +25,28 @@ describe('prettier-formatter-gitlab cli', () => {
const content = readFileSync(CODE_QUALITY_FILENAME);
expect(content.toString()).toMatchSnapshot();
});

it('should work with the `-c` or `-l` flag', async () => {
const consoleSpy = spyOn(console, 'log');
consoleSpy.mockImplementation(() => null);
process.env.PRETTIER_CODE_QUALITY_REPORT = CODE_QUALITY_FILENAME;

// prettier -c path/to/folder/
const outputCheck = `Checking formatting...
[warn] test/dirty-2.js
[warn] test/dirty.js
[warn] Code style issues found in 3 files. Run Prettier to fix.
`;

// prettier -l path/to/folder/
const outputList = `test/dirty-2.js
test/dirty.js
`;

await prettierFormatterGitLab(outputCheck);
await prettierFormatterGitLab(outputList);
expect(consoleSpy).toHaveBeenCalledTimes(12);
expect(consoleSpy.mock.calls.slice(0, 6)).toEqual(consoleSpy.mock.calls.slice(6, 12));
consoleSpy.mockRestore();
});
});

0 comments on commit 019c35a

Please sign in to comment.