Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping #38

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ name: Main CI
on:
workflow_dispatch:
inputs:
distTag:
description: 'Dist tag to publish'
required: true
default: 'latest'
distTag:
description: "Dist tag to publish"
required: true
default: "latest"
pull_request:
push:
branches:
- 'main'
- "main"

jobs:
build_and_testing:
Expand All @@ -21,22 +21,16 @@ jobs:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false

- name: Restore cache
uses: actions/cache@v2
with:
path: ~/.yarn
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v4
with:
always-auth: true
node-version: 12
cache: "yarn"
node-version: 20

- name: Creates local .npmrc
if: ${{github.event.inputs.distTag == 'latest'}}
Expand All @@ -45,6 +39,9 @@ jobs:
- name: Install
run: yarn install --frozen-lockfile

- name: Check Prettier Formatting
run: yarn prettier:check

- name: Build & Testing
run: |
yarn build
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
transforms/*.js
bin/*.js
dist
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

npx lint-staged
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/dist/**
**/__testfixtures__/**
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
152 changes: 76 additions & 76 deletions bin/__tests__/lingui-codemod.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
let gitStatusReturnValue;
let execaReturnValue;
let gitStatusReturnValue: boolean | Error;
let execaReturnValue: { failed: boolean; stderr?: Error };

jest.setMock('execa', {
sync: () => execaReturnValue
jest.setMock("execa", {
sync: () => execaReturnValue,
});

jest.setMock('is-git-clean', {
jest.setMock("is-git-clean", {
sync: () => {
if (typeof gitStatusReturnValue === 'boolean') {
if (typeof gitStatusReturnValue === "boolean") {
return gitStatusReturnValue;
}
throw gitStatusReturnValue;
}
},
});

import fs from "fs";
Expand All @@ -20,190 +20,190 @@ import {
runTransform,
jscodeshiftExecutable,
transformerDirectory,
checkGitStatus
} from '../cli';
checkGitStatus,
} from "../cli";

describe('check-git-status', () => {
it('does not exit and output any logs when git repo is clean', () => {
describe("check-git-status", () => {
it("does not exit and output any logs when git repo is clean", () => {
gitStatusReturnValue = true;
console.log = jest.fn();
// @ts-ignore
process.exit = jest.fn();
checkGitStatus();
expect(console.log).not.toBeCalled();
expect(process.exit).not.toBeCalled();
expect(console.log).not.toHaveBeenCalled();
expect(process.exit).not.toHaveBeenCalled();
});

it('does not exit and output any logs when not a git repo', () => {
it("does not exit and output any logs when not a git repo", () => {
const err = new Error();
// @ts-ignore
err.stderr = 'Not a git repository';
err.stderr = "Not a git repository";
gitStatusReturnValue = err;
console.log = jest.fn();
// @ts-ignore
process.exit = jest.fn();
checkGitStatus();
expect(console.log).not.toBeCalled();
expect(process.exit).not.toBeCalled();
expect(console.log).not.toHaveBeenCalled();
expect(process.exit).not.toHaveBeenCalled();
});

it('exits and output logs when git repo is dirty', () => {
it("exits and output logs when git repo is dirty", () => {
gitStatusReturnValue = false;
console.log = jest.fn();
// @ts-ignore
process.exit = jest.fn();
checkGitStatus();
expect(console.log).toBeCalled();
expect(process.exit).toBeCalled();
expect(console.log).toHaveBeenCalled();
expect(process.exit).toHaveBeenCalled();
});

it('exits and output logs when git detection fail', () => {
gitStatusReturnValue = new Error('bum');
it("exits and output logs when git detection fail", () => {
gitStatusReturnValue = new Error("bum");
console.log = jest.fn();
// @ts-ignore
process.exit = jest.fn();
checkGitStatus();
expect(console.log).toBeCalled();
expect(process.exit).toBeCalled();
expect(console.log).toHaveBeenCalled();
expect(process.exit).toHaveBeenCalled();
});

it('does not exit when git repo is dirty and force flag is given', () => {
it("does not exit when git repo is dirty and force flag is given", () => {
gitStatusReturnValue = false;
console.log = jest.fn();
// @ts-ignore
process.exit = jest.fn();
checkGitStatus(true);
expect(console.log).toBeCalledWith(
'WARNING: Git directory is not clean. Forcibly continuing.'
expect(console.log).toHaveBeenCalledWith(
"WARNING: Git directory is not clean. Forcibly continuing.",
);
expect(process.exit).not.toBeCalled();
expect(process.exit).not.toHaveBeenCalled();
});
});

describe('runTransform', () => {
it('finds transformer directory', () => {
describe("runTransform", () => {
it("finds transformer directory", () => {
fs.lstatSync(transformerDirectory);
});

it('finds jscodeshift executable', () => {
it("finds jscodeshift executable", () => {
fs.lstatSync(jscodeshiftExecutable);
});

it('runs jscodeshift for the given transformer', () => {
it("runs jscodeshift for the given transformer", () => {
execaReturnValue = { failed: false };
console.log = jest.fn();
runTransform({
files: 'src',
files: ["src"],
flags: {},
parser: 'flow',
transformer: 'rename-unsafe-xyz'
parser: "flow",
transformer: "rename-unsafe-xyz",
});
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
// eslint-disable-next-line max-len
`Executing command: jscodeshift --verbose=2 --ignore-pattern=**/node_modules/** --parser flow --extensions=jsx,js --transform ${path.join(
transformerDirectory,
'rename-unsafe-xyz.js'
)} src`
"rename-unsafe-xyz.js",
)} src`,
);
});

it('supports jscodeshift flags', () => {
it("supports jscodeshift flags", () => {
execaReturnValue = { failed: false };
console.log = jest.fn();
runTransform({
files: 'folder',
files: ["folder"],
flags: { dry: true },
parser: 'flow',
transformer: 'v2-to-v3'
parser: "flow",
transformer: "v2-to-v3",
});
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
// eslint-disable-next-line max-len
`Executing command: jscodeshift --dry --verbose=2 --ignore-pattern=**/node_modules/** --parser flow --extensions=jsx,js --transform ${path.join(
transformerDirectory,
'v2-to-v3.js'
)} folder`
"v2-to-v3.js",
)} folder`,
);
});

it('supports typescript parser', () => {
it("supports typescript parser", () => {
execaReturnValue = { failed: false };
console.log = jest.fn();
runTransform({
files: 'folder',
files: ["folder"],
flags: { dry: true },
parser: 'tsx',
transformer: 'rename-unsafe-lifecycles'
parser: "tsx",
transformer: "rename-unsafe-lifecycles",
});
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
// eslint-disable-next-line max-len
`Executing command: jscodeshift --dry --verbose=2 --ignore-pattern=**/node_modules/** --parser tsx --extensions=tsx,ts,jsx,js --transform ${path.join(
transformerDirectory,
'rename-unsafe-lifecycles.js'
)} folder`
"rename-unsafe-lifecycles.js",
)} folder`,
);
});

it('supports jscodeshift custom arguments', () => {
it("supports jscodeshift custom arguments", () => {
execaReturnValue = { failed: false };
console.log = jest.fn();
runTransform({
files: 'folder',
files: ["folder"],
flags: {
dry: true,
jscodeshift: 'verbose=2 --printOptions=\'{"quote":"double"}\''
jscodeshift: 'verbose=2 --printOptions=\'{"quote":"double"}\'',
},
parser: 'babel',
transformer: 'v2-to-v3'
parser: "babel",
transformer: "v2-to-v3",
});
expect(console.log).toBeCalledWith(
expect(console.log).toHaveBeenCalledWith(
// eslint-disable-next-line max-len
`Executing command: jscodeshift --dry --verbose=2 --ignore-pattern=**/node_modules/** --parser babel --extensions=jsx,js --transform ${path.join(
transformerDirectory,
'v2-to-v3.js'
)} verbose=2 --printOptions='{"quote":"double"}' folder`
"v2-to-v3.js",
)} verbose=2 --printOptions='{"quote":"double"}' folder`,
);
});

it('supports remove-unused-imports flag that runs a codemod to clean all unused imports', () => {
it("supports remove-unused-imports flag that runs a codemod to clean all unused imports", () => {
execaReturnValue = { failed: false };
console.log = jest.fn();
runTransform({
files: 'folder',
files: ["folder"],
flags: {
removeUnusedImports: true,
},
parser: 'babel',
transformer: 'v2-to-v3'
parser: "babel",
transformer: "v2-to-v3",
});
expect(console.log).toBeCalledTimes(2)
expect(console.log).toHaveBeenCalledTimes(2);
// @ts-ignore
expect(console.log.mock.calls).toEqual([
[
`Executing command: jscodeshift --verbose=2 --ignore-pattern=**/node_modules/** --parser babel --extensions=jsx,js --transform ${path.join(
transformerDirectory,
'v2-to-v3.js'
)} folder`
"v2-to-v3.js",
)} folder`,
],
[
`Executing command: jscodeshift --verbose=2 --ignore-pattern=**/node_modules/** --parser babel --extensions=jsx,js --transform ${path.join(
transformerDirectory,
'remove-unused-imports.js'
)} folder`
]
"remove-unused-imports.js",
)} folder`,
],
]);
});

it('rethrows jscodeshift errors', () => {
const transformerError = new Error('bum');
it("rethrows jscodeshift errors", () => {
const transformerError = new Error("bum");
execaReturnValue = { failed: true, stderr: transformerError };
console.log = jest.fn();
expect(() => {
runTransform({
files: 'src',
files: ["src"],
flags: {},
parser: 'flow',
transformers: ['tape']
parser: "flow",
transformer: "tape",
});
}).toThrowError(transformerError);
}).toThrow(transformerError);
});
});
Loading