Skip to content

Commit

Permalink
Improve code quality and write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Sep 20, 2019
1 parent db44db4 commit 43178a7
Show file tree
Hide file tree
Showing 3,599 changed files with 255,504 additions and 785,580 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
5 changes: 5 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
- name: Installing NPM
run: npm install

- name: Run tests and send coverage
run: |
npm test
curl -s https://codecov.io/bash | bash -s -- -t ${{secrets.CODECOV_TOKEN}} -f coverage/clover.xml -n github-actions-codecov
- name: Installing PHP with extensions and custom config
run: node lib/install.js
env:
Expand Down
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Explicitly not ignoring node_modules so that they are included in package downloaded by runner
!node_modules/
__tests__/runner/*

# Rest of the file pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/
97 changes: 97 additions & 0 deletions __tests__/features.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as features from '../src/features';

let valid_extensions = ['xdebug', 'pcov'];
jest.mock('../src/pecl', () => ({
checkPECLExtension: jest.fn().mockImplementation(extension => {
return valid_extensions.indexOf(extension) !== -1;
})
}));

describe('Features tests', () => {
it('checking addExtensionOnWindows', async () => {
let win32: string = await features.addExtension(
'xdebug, pcov',
'7.2',
'win32'
);
expect(win32).toContain(
'Install-PhpExtension xdebug -MinimumStability stable'
);
expect(win32).toContain(
'Install-PhpExtension pcov -MinimumStability stable'
);
win32 = await features.addExtension('xdebug, pcov', '7.4', 'win32');
expect(win32).toContain(
'Install-PhpExtension xdebug -MinimumStability alpha'
);
expect(win32).toContain(
'Install-PhpExtension pcov -MinimumStability alpha'
);

win32 = await features.addExtension('DoesNotExist', '7.2', 'win32');
expect(win32).not.toContain(
'Install-PhpExtension DoesNotExist -MinimumStability stable'
);
});
it('checking addExtensionOnLinux', async () => {
let linux: string = await features.addExtension(
'xdebug, pcov',
'7.2',
'linux'
);
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.2-xdebug'
);
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.2-pcov'
);
});
it('checking addExtensionOnDarwin', async () => {
let darwin: string = await features.addExtension(
'xdebug, pcov',
'7.2',
'darwin'
);
expect(darwin).toContain('sudo pecl install xdebug');
expect(darwin).toContain('sudo pecl install pcov');

darwin = await features.addExtension('DoesNotExist', '7.2', 'darwin');
expect(darwin).not.toContain('sudo pecl install DoesNotExist');
});

it('checking addINIValuesOnWindows', async () => {
let win32: string = await features.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'win32'
);
expect(win32).toContain(
'Add-Content C:\\tools\\php$version\\php.ini "post_max_size=256M"'
);
expect(win32).toContain(
'Add-Content C:\\tools\\php$version\\php.ini "short_open_tag=On"'
);
expect(win32).toContain(
'Add-Content C:\\tools\\php$version\\php.ini "date.timezone=Asia/Kolkata"'
);
});

it('checking addINIValuesOnLinux', async () => {
let linux: string = await features.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'linux'
);
expect(linux).toContain('echo "post_max_size=256M" >> $ini_file');
expect(linux).toContain('echo "short_open_tag=On" >> $ini_file');
expect(linux).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');
});

it('checking addINIValuesOnDarwin', async () => {
let darwin: string = await features.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'darwin'
);
expect(darwin).toContain('echo "post_max_size=256M" >> $ini_file');
expect(darwin).toContain('echo "short_open_tag=On" >> $ini_file');
expect(darwin).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');
});
});
6 changes: 0 additions & 6 deletions __tests__/install.test.ts

This file was deleted.

93 changes: 93 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as fs from 'fs';
import * as path from 'path';
import * as utils from '../src/utils';
import * as pecl from '../src/pecl';

let valid_extensions = ['xdebug', 'pcov'];
jest.mock('../src/pecl', () => ({
checkPECLExtension: jest.fn().mockImplementation(extension => {
return valid_extensions.indexOf(extension) !== -1;
})
}));

async function cleanup(path: string): Promise<void> {
fs.unlink(path, error => {
if (error) {
console.log(error);
}
});
}

describe('Utils tests', () => {
it('checking getInput', async () => {
process.env['test'] = 'setup-php';
expect(await utils.getInput('test', false)).toBe('setup-php');
expect(await utils.getInput('DoesNotExist', false)).toBe('');
});

it('checking asyncForEach', async () => {
let array: Array<number> = [1, 2, 3, 4];
let sum: number = 0;
await utils.asyncForEach(array, function(num: number): void {
sum += num;
});
expect(sum).toBe(10);
});

it('checking readScripts', async () => {
let rc: string = fs.readFileSync(
path.join(__dirname, '../src/7.4.sh'),
'utf8'
);
let darwin: string = fs.readFileSync(
path.join(__dirname, '../src/darwin.sh'),
'utf8'
);
let linux: string = fs.readFileSync(
path.join(__dirname, '../src/linux.sh'),
'utf8'
);
let win32: string = fs.readFileSync(
path.join(__dirname, '../src/win32.ps1'),
'utf8'
);
expect(rc).toBe(await utils.readScript('darwin.sh', '7.4', 'darwin'));
expect(darwin).toBe(await utils.readScript('darwin.sh', '7.3', 'darwin'));
expect(linux).toBe(await utils.readScript('linux.sh', '7.3', 'linux'));
expect(win32).toBe(await utils.readScript('win32.ps1', '7.3', 'win32'));
});

it('checking writeScripts', async () => {
let testString: string = 'sudo apt-get install php';
await utils.writeScript('test.sh', '10', testString);
await fs.readFile(path.join(__dirname, '../10test.sh'), function(
error: any,
data: Buffer
) {
expect(testString).toBe(data.toString());
});
await cleanup('./10test.sh');
});

it('checking extensionArray', async () => {
expect(await utils.extensionArray('a, b, php_c, php-d')).toEqual([
'a',
'b',
'c',
'd'
]);
});

it('checking INIArray', async () => {
expect(await utils.INIArray('a=1, b=2, c=3')).toEqual([
'a=1',
'b=2',
'c=3'
]);
});

it('checking checkPECLExtension', async () => {
expect(await pecl.checkPECLExtension('extensionDoesNotExist')).toBe(false);
expect(await pecl.checkPECLExtension('xdebug')).toBe(true);
});
});
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = {
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}
verbose: true,
collectCoverage: true
};
Loading

0 comments on commit 43178a7

Please sign in to comment.