Permalink
Browse files

replaced tmpdir magic with fixtures

  • Loading branch information...
1 parent 9109cd3 commit 63039968bfce30a3985eecc3c387cb4e93825990 @puzrin puzrin committed Feb 17, 2013
Showing with 17 additions and 62 deletions.
  1. +2 −2 package.json
  2. +2 −0 test/fixtures/hello
  3. +2 −0 test/fixtures/invalid
  4. +3 −0 test/fixtures/recursive
  5. +8 −60 test/fromfile.js
View
@@ -17,10 +17,10 @@
"test": "make test"
},
- "dependencies" : {
+ "dependencies" : {
"underscore" : "~1.4.3",
"underscore.string" : "~2.3.1"
},
- "devDependencies" : { "mocha": "~1.8.1" },
+ "devDependencies" : { "mocha": "*" },
"engines" : { "node": ">= 0.6.0" }
}
View
@@ -0,0 +1,2 @@
+hello world!
+
View
@@ -0,0 +1,2 @@
+@no-such-path
+
View
@@ -0,0 +1,3 @@
+-a
+A
+@hello
View
@@ -6,72 +6,20 @@ var assert = require('assert');
var ArgumentParser = require('../lib/argparse').ArgumentParser;
-var fs = require('fs');
-var os = require('os');
-var path = require('path');
-var assert = require('assert');
-var _ = require('underscore');
-_.str = require('underscore.string');
-
-var oldcwd = process.cwd();
-
-function setup_tempdir() {
- // setup a temporary directory:
- // /tmp/argparse_6732
- //
- // For 0.6 node use '/tmp' as workaround for travis-ci
- var tdir = path.join(
- os.tmpDir ? os.tmpDir() : '/tmp',
- 'argparse_' + Math.floor(Math.random() * 10000)
- );
+var assert = require('assert');
+var path = require('path');
+var _ = require('underscore');
+_.str = require('underscore.string');
- try {
- fs.mkdirSync(tdir);
- } catch (error) {
- if (!error.message.match(/EEXIST/)) {
- throw error;
- }
- }
- oldcwd = process.cwd();
- process.chdir(tdir);
- // console.log('Now in ' + process.cwd());
- return oldcwd;
-}
-function teardown_tempdir(oldcwd) {
- // remove the temp dir
- var tdir = process.cwd();
- process.chdir(oldcwd);
- if (_.str.startsWith(tdir, os.tmpDir ? os.tmpDir() : '/tmp')) {
- var dirls = fs.readdirSync(tdir);
- //console.log(tdir, dirls)
- dirls.forEach(function (f) {
- fs.unlinkSync(path.join(tdir, f));
- });
- fs.rmdirSync(tdir);
- //console.log('Removed ' + tdir);
- }
-}
-
-function setup_files() {
- var file_texts = [['hello', 'hello world!\n'],
- ['recursive', '-a\n', 'A\n', '@hello'],
- ['invalid', '@no-such-path\n']];
-
- oldcwd = setup_tempdir();
- // write the test files
- file_texts.forEach(function (tpl) {
- var filename = tpl[0];
- var data = tpl.slice(1).join('');
- fs.writeFileSync(filename, data);
- });
-}
+var orig_cwd = process.cwd();
describe('from file', function () {
var parser;
var args;
before(function () {
- setup_files();
+ orig_cwd = process.cwd();
+ process.chdir(path.normalize('./test/fixtures'));
});
beforeEach(function () {
parser = new ArgumentParser({debug: true, fromfilePrefixChars: '@'});
@@ -80,7 +28,7 @@ describe('from file', function () {
parser.addArgument(['y'], {nargs: '+'});
});
after(function () {
- teardown_tempdir(oldcwd);
+ process.chdir(orig_cwd);
});
it("test reading arguments from a file", function () {
args = parser.parseArgs(['X', 'Y']);

0 comments on commit 6303996

Please sign in to comment.