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

TypeScript Unit Tests #533

Merged
merged 16 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Create stashListTask to reuse the new logTask
  • Loading branch information
steveukx committed Dec 1, 2020
commit 4231be7134101be48f22e11c99148fe3f27ad8e2
34 changes: 12 additions & 22 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {pullTask} = require('./lib/tasks/pull');
const {pushTagsTask, pushTask} = require('./lib/tasks/push');
const {addRemoteTask, getRemotesTask, listRemotesTask, remoteTask, removeRemoteTask} = require('./lib/tasks/remote');
const {getResetMode, resetTask} = require('./lib/tasks/reset');
const {stashListTask} = require('./lib/tasks/stash-list');
const {statusTask} = require('./lib/tasks/status');
const {addSubModuleTask, initSubModuleTask, subModuleTask, updateSubModuleTask} = require('./lib/tasks/sub-module');
const {addAnnotatedTagTask, addTagTask, tagListTask} = require('./lib/tasks/tag');
Expand Down Expand Up @@ -151,26 +152,15 @@ Git.prototype.status = function () {

/**
* List the stash(s) of the local repo
*
* @param {Object|Array} [options]
* @param {Function} [then]
*/
Git.prototype.stashList = function (options, then) {
var handler = trailingFunctionArgument(arguments);
var opt = (handler === then ? options : null) || {};

var splitter = opt.splitter || requireResponseHandler('ListLogSummary').SPLITTER;
var command = ["stash", "list", "--pretty=format:"
+ requireResponseHandler('ListLogSummary').START_BOUNDARY
+ "%H %ai %s%d %aN %ae".replace(/\s+/g, splitter)
+ requireResponseHandler('ListLogSummary').COMMIT_BOUNDARY
];

if (Array.isArray(opt)) {
command = command.concat(opt);
}

return this._run(command, handler, {parser: Git.responseParser('ListLogSummary', splitter)});
Git.prototype.stashList = function (options) {
return this._runTask(
stashListTask(
trailingOptionsArgument(arguments) || {},
filterArray(options) && options || []
),
trailingFunctionArgument(arguments),
);
};

/**
Expand All @@ -180,9 +170,9 @@ Git.prototype.stashList = function (options, then) {
* @param {Function} [then]
*/
Git.prototype.stash = function (options, then) {
return this._run(
['stash'].concat(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
return this._runTask(
straightThroughStringTask(['stash', ...getTrailingOptions(arguments)]),
trailingFunctionArgument(arguments),
);
};

Expand Down
3 changes: 1 addition & 2 deletions src/lib/tasks/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type LogOptions<T = DefaultLogFields> = {
to?: string;
};

function prettyFormat(format: Object, splitter: string) {
function prettyFormat(format: Object, splitter: string): [string[], string] {
const fields: string[] = [];
const formatStr: string[] = [];

Expand Down Expand Up @@ -111,7 +111,6 @@ export function parseLogOptions<T extends Options>(opt: LogOptions<T> = {}, cust
};
}


export function logTask<T>(splitter: string, fields: string[], customArgs: string[]): StringTask<LogResult<T>> {
return {
commands: ['log', ...customArgs],
Expand Down
15 changes: 15 additions & 0 deletions src/lib/tasks/stash-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LogOptions, LogResult } from '../../../typings';
import { createListLogSummaryParser } from '../parsers/parse-list-log-summary';
import { StringTask } from '../types';
import { parseLogOptions } from './log';

export function stashListTask(opt: LogOptions = {}, customArgs: string[]): StringTask<LogResult> {
const options = parseLogOptions<any>(opt);
const parser = createListLogSummaryParser(options.splitter, options.fields);

return {
commands: ['stash', 'list', ...options.commands, ...customArgs],
format: 'utf-8',
parser,
};
}
4 changes: 2 additions & 2 deletions src/lib/tasks/task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TaskConfigurationError } from '../errors/task-configuration-error';
import { BufferTask, EmptyTaskParser, SimpleGitTask, SimpleGitTaskConfiguration, StringTask } from '../types/tasks';
import { BufferTask, EmptyTaskParser, SimpleGitTask, SimpleGitTaskConfiguration, StringTask } from '../types';

export const EMPTY_COMMANDS: [] = [];

Expand All @@ -9,7 +9,7 @@ export type EmptyTask<RESPONSE = void> = SimpleGitTaskConfiguration<RESPONSE, 'u
};


export function adhocExecTask<R> (parser: () => R): StringTask<R> {
export function adhocExecTask<R>(parser: () => R): StringTask<R> {
return {
commands: EMPTY_COMMANDS,
format: 'utf-8',
Expand Down
25 changes: 25 additions & 0 deletions test/unit/log.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,33 @@ ${START_BOUNDARY}jkl${COMMIT_BOUNDARY}
const splitOn = {
PIPES: '||',
SEMI: ';',
SEMIS: ';;;;;',
};

it('three item stash', async () => {
const parser = createListLogSummaryParser(splitOn.SEMIS);
const actual = parser(`

${ START_BOUNDARY }aaa;;;;;2018-09-13 06:52:30 +0100;;;;;WIP on master: 2942035 blah (refs/stash);;;;;Steve King;;;;;steve@mydev.co${ COMMIT_BOUNDARY }
${ START_BOUNDARY }bbb;;;;;2018-09-13 06:52:10 +0100;;;;;WIP on master: 2942035 blah;;;;;Steve King;;;;;steve@mydev.co${ COMMIT_BOUNDARY }
${ START_BOUNDARY }ccc;;;;;2018-09-13 06:48:22 +0100;;;;;WIP on master: 2942035 blah;;;;;Steve King;;;;;steve@mydev.co${ COMMIT_BOUNDARY }

`);

expect(actual).toEqual(like({
total: 3,
latest: like({
hash: 'aaa'
}),
all: [
like({ hash: 'aaa '}),
like({ hash: 'bbb '}),
like({ hash: 'ccc '}),
]
}));

});

it('parses regular log', () => {
const parser = createListLogSummaryParser(splitOn.PIPES, ['hash', 'message']);
actual = parser(`
Expand Down
55 changes: 55 additions & 0 deletions test/unit/stash-list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { SimpleGit } from '../../typings';
import { assertExecutedCommands, like, newSimpleGit } from './__fixtures__';
import { COMMIT_BOUNDARY, SPLITTER, START_BOUNDARY } from '../../src/lib/parsers/parse-list-log-summary';

const {closeWithSuccess, restore} = require('./include/setup');

describe('stashList', () => {

let git: SimpleGit;
let callback: jest.Mock;

beforeEach(() => {
git = newSimpleGit();
callback = jest.fn();
});

afterEach(() => restore());

it('with no stash', async () => {
const expected = like({
total: 0,
all: [],
});
const queue = git.stashList(callback);
closeWithSuccess();

expect(await queue).toEqual(expected);
expect(callback).toHaveBeenCalledWith(null, expected);
});

it('commands - default', async () => {
git.stashList();
await closeWithSuccess();

assertExecutedCommands(
'stash',
'list',
`--pretty=format:${START_BOUNDARY}%H${SPLITTER}%aI${SPLITTER}%s${SPLITTER}%D${SPLITTER}%b${SPLITTER}%aN${SPLITTER}%ae${COMMIT_BOUNDARY}`
);
});

it('commands - custom splitter', async () => {
const splitter = ';;';

git.stashList({splitter});
await closeWithSuccess();

assertExecutedCommands(
'stash',
'list',
`--pretty=format:${START_BOUNDARY}%H${splitter}%aI${splitter}%s${splitter}%D${splitter}%b${splitter}%aN${splitter}%ae${COMMIT_BOUNDARY}`
);
});

});
81 changes: 0 additions & 81 deletions test/unit/test-stash-list.js

This file was deleted.

1 change: 1 addition & 0 deletions typings/simple-git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ export interface SimpleGit {
* List the stash(s) of the local repo
*/
stashList(options?: types.TaskOptions, callback?: types.SimpleGitTaskCallback<resp.LogResult>): Response<resp.LogResult>;
stashList(callback?: types.SimpleGitTaskCallback<resp.LogResult>): Response<resp.LogResult>;

/**
* Show the working tree status.
Expand Down