Skip to content

Commit

Permalink
Merge pull request #5 from julien-capellari/feature/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
julien-capellari committed Mar 17, 2021
2 parents 815fcf7 + 16acdb3 commit 1e7f0b7
Show file tree
Hide file tree
Showing 18 changed files with 9,625 additions and 1,014 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,14 @@
version: 2

updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

# Maintain dependencies for npm/yarn
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"
124 changes: 114 additions & 10 deletions .github/workflows/build.yml
Expand Up @@ -10,8 +10,37 @@ on:
- master

jobs:
unit-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
submodules: true

- uses: actions/setup-node@v2.1.5
with:
node-version: 12.x

- name: Setup Cache
id: setup-cache
run: echo "::set-output name=yarn-cache::$(yarn cache dir)"

- uses: actions/cache@v2.1.4
with:
path: ${{ steps.setup-cache.outputs.yarn-cache }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-

- name: Install
run: yarn install --frozen-lockfile

- name: Test
run: yarn test

build:
runs-on: ubuntu-latest
needs: [unit-tests]

steps:
- uses: actions/checkout@v2
Expand All @@ -31,22 +60,97 @@ jobs:
restore-keys: ${{ runner.os }}-yarn-

- name: Install
run: yarn install
run: yarn install --frozen-lockfile

- name: Build
run: yarn build

- name: Commit built files
if: github.event_name == 'push'
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@users.noreply.github.com'
git add dist
git commit -m "Automatic build" && git push || true
- name: Archive build artifacts
if: github.event_name != 'push'
uses: actions/upload-artifact@v2
with:
name: bundle
path: dist

action-tests:
runs-on: ubuntu-latest
needs: [build]

strategy:
matrix:
include:
- branch: test/test-a-affected
workspace: test-a
base: master
affected: true

- branch: test/test-a-affected
workspace: test-b
base: master
affected: false

- branch: test/tags
workspace: test-a
base: test-tag
affected: true

- branch: test/tags
workspace: test-b
base: test-tag
affected: false

steps:
- uses: actions/checkout@v2

- name: Download build artifacts
uses: actions/download-artifact@v2
with:
name: bundle
path: dist

- name: Checkout test repository
uses: actions/checkout@v2
with:
repository: julien-capellari/test-is-workspace-affected
path: test-repo
ref: ${{ matrix.branch }}

- name: Test action
id: affected
uses: ./
with:
project-root: test-repo
workspace: ${{ matrix.workspace }}
base: ${{ matrix.base }}

- name: Should be affected
if: ${{ matrix.affected && !steps.affected.outputs.affected }}
run: |
echo "::error::Test failed: workspace ${{ matrix.workspace }} should be affected"
exit 1
- name: Should not be affected
if: ${{ !matrix.affected && steps.affected.outputs.affected }}
run: |
echo "::error::Test failed: workspace ${{ matrix.workspace }} should not be affected"
exit 1
deploy:
if: github.event_name == 'push'
runs-on: ubuntu-latest
needs: [action-tests]

steps:
- uses: actions/checkout@v2

- name: Download build artifacts
uses: actions/download-artifact@v2
with:
name: bundle
path: dist

- name: Commit bundle files
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@users.noreply.github.com'
git add dist
git commit -m "Automatic build" && git push || true
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "tests/project"]
path = tests/project
url = git@github.com:julien-capellari/test-is-workspace-affected.git
4 changes: 1 addition & 3 deletions .idea/is-workspace-affected.iml

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

1 change: 1 addition & 0 deletions .idea/vcs.xml

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

6,239 changes: 6,237 additions & 2 deletions dist/bundle.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions jest.config.json
@@ -0,0 +1,18 @@
{
"testEnvironment": "node",
"roots": [
"<rootDir>/tests"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": ".+\\.(test|spec)\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
13 changes: 9 additions & 4 deletions package.json
Expand Up @@ -3,17 +3,22 @@
"version": "1.0.0",
"license": "MIT",
"scripts": {
"build": "webpack build"
"build": "webpack build",
"build:dev": "webpack build --mode=developement",
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.2.6",
"@yarnpkg/cli": "^2.4.1",
"@yarnpkg/core": "^2.4.0",
"@yarnpkg/fslib": "^2.4.0",
"@actions/glob": "^0.1.1",
"simple-git": "^2.36.2"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"@types/node": "^12.20.6",
"jest": "^26.6.3",
"ts-jest": "^26.5.3",
"ts-loader": "^8.0.18",
"ts-node": "^9.1.1",
"typescript": "^4.2.3",
"webpack": "^5.26.0",
"webpack-cli": "^4.5.0"
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
@@ -1,8 +1,7 @@
import core from '@actions/core';
import { npath } from '@yarnpkg/fslib';
import * as core from '@actions/core';
import simpleGit from 'simple-git';

import yarn from './yarn';
import { Project } from './project';

(async () => {
try {
Expand All @@ -15,10 +14,10 @@ import yarn from './yarn';

// Load project
const git = simpleGit({ baseDir: inputs.projectRoot });
const project = await yarn.getProject(inputs.projectRoot);
const project = await Project.loadProject(inputs.projectRoot);

// Get workspace
const workspace = project.workspaces.find(wks => wks.manifest.name?.name === inputs.workspace);
const workspace = await project.getWorkspace(inputs.workspace);

if (!workspace) {
return core.setFailed(`Workspace ${inputs.workspace} not found.`);
Expand Down Expand Up @@ -47,7 +46,7 @@ import yarn from './yarn';
}

const diff = await core.group('git diff', async () => {
const res = await git.diff(['--name-only', baseRef, '--', npath.fromPortablePath(workspace.cwd)]);
const res = await git.diff(['--name-only', baseRef, '--', workspace.root]);
core.info(res);

return res;
Expand Down
9 changes: 9 additions & 0 deletions src/package.d.ts
@@ -0,0 +1,9 @@
// Types
export interface Package {
name: string;
version?: string;
private?: boolean;
workspaces?: string[];
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
}
43 changes: 43 additions & 0 deletions src/project.ts
@@ -0,0 +1,43 @@
import * as core from '@actions/core';
import * as glob from '@actions/glob';
import * as path from 'path';

import { Workspace } from './workspace';

// Class
export class Project extends Workspace {
// Statics
static async loadProject(root: string): Promise<Project> {
return new Project(root, await this.loadPackage(root));
}

// Methods
private async* generateWorkspaces() {
// Self ;)
yield this;

// Load workspaces
if (this.pkg.workspaces && this.pkg.workspaces.length > 0) {
const patterns = this.pkg.workspaces.map(wks => path.join(this.root, wks, 'package.json'));
const globber = await glob.create(patterns.join('\n'));

for await (let root of globber.globGenerator()) {
root = root.replace(/[\\/]package\.json$/, '');

try {
yield await Workspace.loadWorkspace(root);
} catch (error) {
core.warning(`Unable to load workspace at ${root}: ${error}`);
}
}
}
}

async getWorkspace(name: string): Promise<Workspace | null> {
for await (const wks of this.generateWorkspaces()) {
if (wks.name === name) return wks;
}

return null;
}
}
30 changes: 30 additions & 0 deletions src/workspace.ts
@@ -0,0 +1,30 @@
import { promises as fs } from 'fs';
import * as path from 'path';

import { Package } from './package';

// Class
export class Workspace {
// Constructor
protected constructor(
readonly root: string,
protected readonly pkg: Package
) {}

// Statics
protected static async loadPackage(root: string): Promise<Package> {
const file = path.join(root, 'package.json');
const data = await fs.readFile(file, 'utf-8');

return JSON.parse(data);
}

static async loadWorkspace(root: string): Promise<Workspace> {
return new Workspace(root, await this.loadPackage(root));
}

// Properties
get name(): string {
return this.pkg.name;
}
}
21 changes: 0 additions & 21 deletions src/yarn.ts

This file was deleted.

1 change: 1 addition & 0 deletions tests/project
Submodule project added at aa0065

0 comments on commit 1e7f0b7

Please sign in to comment.