import path from 'path';
import nock from 'nock';
import pathExists from 'path-exists';
import pify from 'pify';
import rimraf from 'rimraf';
import test from 'ava';
import fn from './';

const tmp = path.join(__dirname, 'tmp');

test.afterEach(async () => await pify(rimraf)(tmp));

test('download and build source', async t => {
	const scope = nock('http://foo.com')
		.get('/gifsicle.tar.gz')
		.replyWithFile(200, path.join(__dirname, 'fixtures', 'test.tar.gz'));

	await fn.url('http://foo.com/gifsicle.tar.gz', [
		'autoreconf -ivf',
		`./configure --disable-gifview --disable-gifdiff --prefix="${tmp}" --bindir="${tmp}"`,
		'make install'
	]);

	t.true(scope.isDone());
	t.true(await pathExists(path.join(tmp, 'gifsicle')));
});

test('build source from existing archive', async t => {
	await fn.file(path.join(__dirname, 'fixtures', 'test.tar.gz'), [
		'autoreconf -ivf',
		`./configure --disable-gifview --disable-gifdiff --prefix="${tmp}" --bindir="${tmp}"`,
		'make install'
	]);

	t.true(await pathExists(path.join(tmp, 'gifsicle')));
});

@@ -135,16 +135,49 @@
 imraf';%0A
+import tempfile from 'tempfile';%0A
 import t
@@ -218,46 +218,61 @@
 ';%0A%0A
-const tmp = path.join(__dirname, 'tmp'
+test.beforeEach(t =%3E %7B%0A%09t.context.tmp = tempfile();%0A%7D
 );%0A%0A
@@ -292,18 +292,17 @@
 h(async 
-()
+t
  =%3E awai
@@ -316,16 +316,26 @@
 rimraf)(
+t.context.
 tmp));%0A%0A
@@ -645,32 +645,42 @@
 iff --prefix=%22$%7B
+t.context.
 tmp%7D%22 --bindir=%22
@@ -673,32 +673,42 @@
 p%7D%22 --bindir=%22$%7B
+t.context.
 tmp%7D%22%60,%0A%09%09'make 
@@ -774,32 +774,42 @@
 xists(path.join(
+t.context.
 tmp, 'gifsicle')
@@ -1019,24 +1019,34 @@
 --prefix=%22$%7B
+t.context.
 tmp%7D%22 --bind
@@ -1051,16 +1051,26 @@
 ndir=%22$%7B
+t.context.
 tmp%7D%22%60,%0A
@@ -1127,16 +1127,26 @@
 th.join(
+t.context.
 tmp, 'gi

Build into temporary paths when testing