nathasm
nathasm

Reputation: 2574

Manufacturing a git repo for unit test

I function in node that is supposed to return a list of recently modified (1 day old) files in git. The function basically does something like

git diff --name-only $(git rev-list -n1 --before="1 day ago" HEAD)

or

git log --since="1.day" --name-only --oneline

So, I want to validate that the function does what it is supposed to do. So, in a mocha test, I have:

  var tmp = require('tmp');
  describe('Git test', function() {
  var today = new Date();
  var threedays = new Date();
  var yesterday = new Date();

  threedays.setDate(today.getDate() - 3);
  yesterday.setDate(today.getDate() - 1);    

  beforeEach(function(done) {
    tmp.dir({ template: '/tmp/test-XXXXXX', unsafeCleanup: true },
      function _tempDirCreated(err, path) {
        if (err) { throw err; }
        repo = path;
        sh.exec('cd ' + repo + '; git init');
        sh.exec('touch ' + repo + '/file1');
        sh.exec('touch ' + repo + '/file2');
        //sh.exec('touch ' + repo + '/file3');
        //sh.exec('cd ' + repo + '; git add file3; GIT_COMMITTER_DATE="' + 
        //  threedays.toISOString() +
        //  '" git commit -m "file3" file3 --date ' + threedays.toISOString());
        sh.exec('cd ' + repo + '; git add file2; GIT_COMMITTER_DATE="' +
          yesterday.toISOString() + '" git commit -m "FILE2" file2 --date ' +
          yesterday.toISOString());
        sh.exec('cd ' + repo + '; git add file1; git commit -m "FILE1" file1');
        sessionConfig = new Session(repo, repo);
        done();
      }
    );
  });

What I am noticing is that after I create the git repo, the git commands to only return recently modified files don't seem to return a result. However, if I add file3 (the commented section), the command will work.

What I want to ultimately create is a repo that has old commits and new commits and a command that returns only recently modified files. Any suggestions appreciated!

Upvotes: 1

Views: 72

Answers (1)

nathasm
nathasm

Reputation: 2574

Turns out that getting "yesterday" had some issues in dealing with timezones when converting to an ISO String.

I modified my yesterday to be midnight and now everything works.

Upvotes: 1

Related Questions