From 4b4c8030c8174cfe02ad26c7ef16676388084fd8 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Sun, 17 Mar 2013 17:00:22 +0000 Subject: [PATCH] locked down the signature of Repo#sync with unit tests and an accurate representation in the README --- README.md | 8 ++++---- package.json | 1 + src/repo.coffee | 18 +++++++++++++----- test/repo.test.coffee | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c0f5027..e31eca0 100644 --- a/README.md +++ b/README.md @@ -143,14 +143,14 @@ Commit some changes. ### `Repo#checkout(treeish, callback)` `git checkout ` -### `Repo#sync([remote_name, ]branch, callback)` +### `Repo#sync([[remote, ]branch, ]callback)` Sync the current branch with the remote, keeping all local changes intact. The following steps are carried out: `stash`, `pull`, `push`, `stash pop`. If there were no changes to stash, the last `stash pop` is not executed. - * `remote_name` - `String` - * `branch` - `String` - * `callback` - Receives `(err)`. + * `remote` - `String` (defaults to `origin`). + * `branch` - `String` (defaults to `master`). + * `callback` - Receives `(err)`. ## Commit diff --git a/package.json b/package.json index b90e05c..b5bd4a8 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ , "devDependencies": { "should": "1.2.x" , "mocha": "1.x.x" + , "sinon": "1.6.x" , "coffee-script": "1.6.x" } diff --git a/src/repo.coffee b/src/repo.coffee index 89b7523..7922dfd 100644 --- a/src/repo.coffee +++ b/src/repo.coffee @@ -299,18 +299,26 @@ module.exports = class Repo # Public: Sync the current branch with the remote. # + # Arguments: ([[remote_name, ]branch_name, ]callback) + # + # remote_name - String (optional). + # branch_name - String. # callback - Receives `(err)`. # - sync: (remote_name, branch, callback) -> - [remote_name, callback, branch] = ['origin', branch, remote_name] if !callback - [remote_name, callback, branch] = ['origin', remote_name, []] if !branch + sync: (remote_name, branch_name, callback) -> + + # handle 'curried' arguments + [remote, branch] = [remote_name, branch_name] if typeof callback is "function" + [remote, branch, callback] = ["origin", remote_name, branch_name] if typeof branch_name is "function" + [remote, branch, callback] = ["origin", "master", remote_name] if typeof remote_name is "function" + @status (err, status) => return callback err if err @git "stash", {}, ["save"], (err) => return callback err if err - @git "pull", {}, [remote_name, branch], (err) => + @git "pull", {}, [remote, branch], (err) => return callback err if err - @git "push", {}, [remote_name, branch], (err) => + @git "push", {}, [remote, branch], (err) => return callback err if err if not status?.clean @git "stash", {}, ["pop"], (err) => diff --git a/test/repo.test.coffee b/test/repo.test.coffee index 2391cf0..1fe7c0c 100644 --- a/test/repo.test.coffee +++ b/test/repo.test.coffee @@ -1,4 +1,5 @@ should = require 'should' +sinon = require 'sinon' fs = require 'fs' fixtures = require './fixtures' git = require '../src' @@ -13,6 +14,43 @@ Status = require '../src/status' {exec} = require 'child_process' describe "Repo", -> + describe "#sync", -> + describe "when passed curried arguments", -> + repo = fixtures.branched + remote = branch = "" + + before -> + sinon.stub repo, "git", (command, opts, args, callback) -> + if command is "pull" + remote = args[0] + branch = args[1] + callback? null + sinon.stub repo, "status", (callback) -> + callback? null, clean: no + + after -> + repo.git.restore() + repo.status.restore() + + it "passes through the correct parameters when nothing is omitted", (done) -> + repo.sync "github", "my-branch", -> + remote.should.eql "github" + branch.should.eql "my-branch" + done() + + it "passes through the correct parameters when remote_name is omitted", (done) -> + repo.sync "my-branch", -> + remote.should.eql "origin" + branch.should.eql "my-branch" + done() + + it "passes through the correct parameters when remote_name and branch are omitted", (done) -> + repo.sync -> + remote.should.eql "origin" + branch.should.eql "master" + done() + + describe "#identify", -> describe "when asked to set the identity's name and email", -> repo = fixtures.branched