Skip to content

Commit

Permalink
add source-root input. closes #320
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Matrosov committed Jan 25, 2023
1 parent 362d848 commit 165f0c6
Show file tree
Hide file tree
Showing 12 changed files with 64,435 additions and 41,832 deletions.
93 changes: 92 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as cp from 'child_process';
import * as path from 'path';
import * as process from 'process';
import {test} from '@jest/globals';
import {expect, test} from '@jest/globals';
import {ZipInputs, zipSources} from '../src/main';
import archiver from 'archiver';

// This test will run only in fully configured env and creates real VM
// in the Yandex Cloud, so it will be disabled in CI/CD. You can enable it to test locally.
Expand All @@ -25,3 +27,92 @@ test.skip('test runs', () => {
}
console.log(res?.toString());
});

describe('zipSources', function () {
test('it should add files from include', async () => {
const archive = archiver('zip', {zlib: {level: 9}});
const inputs: ZipInputs = {
include: ['./src'],
excludePattern: [],
sourceRoot: '.',
};

const entries: archiver.EntryData[] = [];
archive.on('entry', e => entries.push(e));
await zipSources(inputs, archive);

const allStartWithSrc = entries.every(e => e.name.startsWith('./src'));
expect(allStartWithSrc).toBeTruthy();
});

test('it should drop files from if they do not match include patterns', async () => {
const archive = archiver('zip', {zlib: {level: 9}});
const inputs: ZipInputs = {
include: ['./src/*.js'],
excludePattern: [],
sourceRoot: '.',
};

const entries: archiver.EntryData[] = [];
archive.on('entry', e => entries.push(e));
await zipSources(inputs, archive);

const allStartWithSrc = entries.every(e => e.name.startsWith('./src'));
expect(allStartWithSrc).toBeTruthy();
expect(entries.length).toBe(1);
expect(entries[0].name).toBe('./src/func.js');
});

test('it should drop files from if they match exclude patterns', async () => {
const archive = archiver('zip', {zlib: {level: 9}});
const inputs: ZipInputs = {
include: ['./src'],
excludePattern: ['*.txt'],
sourceRoot: '.',
};

const entries: archiver.EntryData[] = [];
archive.on('entry', e => entries.push(e));
await zipSources(inputs, archive);

const allStartWithSrc = entries.every(e => e.name.startsWith('./src'));
expect(allStartWithSrc).toBeTruthy();
expect(entries.length).toBe(2);
});

test('it should drop folder prefix if sourceRoot provided', async () => {
const archive = archiver('zip', {zlib: {level: 9}});
const inputs: ZipInputs = {
include: ['.'],
excludePattern: [],
sourceRoot: './src',
};

const entries: archiver.EntryData[] = [];
archive.on('entry', e => entries.push(e));
await zipSources(inputs, archive);

const allStartWithSrc = entries.every(e => !e.name.startsWith('./src'));

expect(allStartWithSrc).toBeTruthy();
expect(entries.length).toEqual(3);
});

test('it should respect source root and include only needed files', async () => {
const archive = archiver('zip', {zlib: {level: 9}});
const inputs: ZipInputs = {
include: ['./*.js'],
excludePattern: [],
sourceRoot: './src',
};

const entries: archiver.EntryData[] = [];
archive.on('entry', e => entries.push(e));
await zipSources(inputs, archive);

const allStartWithSrc = entries.every(e => !e.name.startsWith('./src'));
expect(allStartWithSrc).toBeTruthy();
expect(entries.length).toBe(1);
expect(entries[0].name).toBe('./func.js');
});
});
2 changes: 1 addition & 1 deletion __tests__/memory.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {test, expect} from '@jest/globals';
import {expect, test} from '@jest/globals';
import {GB, MB, parseMemory} from '../src/memory';

const mbs = ['mb', 'MB', ' mb'];
Expand Down
11 changes: 0 additions & 11 deletions __tests__/package.json

This file was deleted.

4 changes: 2 additions & 2 deletions __tests__/src/func.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports.handler = async function (event, context) {
module.exports.handler = async function(event, context) {
return {
statusCode: 200,
body: 'Hello World!',
body: "Hello World!"
};
};
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ inputs:
description: 'Exclude patterns for source directory. Multiline'
default: ''
required: false
source-root:
description: 'Path that will considered as function source root.'
required: false
default: '.'
memory:
description: 'Memory in Mb. Pattern: ^\d+Mb$'
default: '128Mb'
Expand Down
Loading

0 comments on commit 165f0c6

Please sign in to comment.