'use strict'

// Node.js built-ins

const fs = require('fs')
const path = require('path')

// foreign modules

const pify = require('pify')
const test = require('ava')

// local modules

const lib = require('../lib/scope')

// this module

const fsp = pify(fs)

test.afterEach((t) => {
  const tempFile = path.join(process.cwd(), lib.CONFIG_FILE)
  fsp.access(tempFile, fs.F_OK | fs.W_OK)
    .then(() => fsp.unlink(tempFile))
    .catch(() => Promise.resolve()) // swallow errors
})

test.serial('read missing .blinkmrc.json', (t) => {
  const cwd = process.cwd()
  return lib.read({ cwd })
    .then(() => {
      t.fail()
    })
    .catch((err) => {
      t.ok(err)
      return Promise.resolve()
    })
})

test('read empty .blinkmrc.json', (t) => {
  const cwd = path.join(__dirname, 'fixtures', 'scope', 'empty')
  return lib.read({ cwd })
    .then(() => {
      t.fail()
    })
    .catch((err) => {
      t.ok(err)
      return Promise.resolve()
    })
})

test('read .blinkmrc.json in same directory', (t) => {
  const cwd = path.join(__dirname, 'fixtures', 'scope')
  return lib.read({ cwd })
    .then((scope) => {
      t.is(scope, 'https://example.com/space')
    })
})

test('read .blinkmrc.json in parent directory', (t) => {
  const cwd = path.join(__dirname, 'fixtures', 'scope', 'sub')
  return lib.read({ cwd })
    .then((scope) => {
      t.is(scope, 'https://example.com/space')
    })
})

test.serial('write invalid scope URL to .blinkmrc.json', (t) => {
  const cwd = process.cwd()
  return lib.write({ cwd, scope: 'abc' })
    .then(() => {
      t.fail()
    })
    .catch((err) => {
      t.ok(err)
      return Promise.resolve()
    })
})

test.serial('write to .blinkmrc.json in same directory', (t) => {
  const cwd = process.cwd()
  return lib.write({ cwd, scope: 'https://example.com/space' })
    .then(() => lib.read({ cwd }))
    .then((scope) => {
      t.is(scope, 'https://example.com/space')
    })
})

test.serial('update to .blinkmrc.json in parent directory', (t) => {
  const cwd = path.join(process.cwd(), 'test')
  return lib.write({ cwd: process.cwd(), scope: 'https://example.com/space' })
    .then(() => lib.write({ cwd, scope: 'https://example.com/abcdef' }))
    .then(() => lib.read({ cwd }))
    .then((scope) => {
      t.is(scope, 'https://example.com/abcdef')
    })
})

@@ -33,33 +33,8 @@
 ns%0A%0A
-const fs = require('fs')%0A
 cons
@@ -108,16 +108,59 @@
 'pify')%0A
+const temp = pify(require('temp').track())%0A
 const te
@@ -238,265 +238,210 @@
 e')%0A
-%0A// this module%0A%0Aconst fsp = pify(fs)%0A%0Atest.afterEach((t) =%3E %7B%0A  const tempFile = path.join(process.cwd(), lib.CONFIG_FILE)%0A  fsp.access(tempFile, fs.F_OK %7C fs.W_OK)%0A    .then(() =%3E fsp.unlink(tempFile))%0A    .catch(() =%3E Promise.resolve()) // swallow errors
+const pkg = require('../package.json')%0A%0A// this module%0A%0Atest.beforeEach((t) =%3E %7B%0A  return temp.mkdir(pkg.name.replace(/%5C//g, '-') + '-')%0A    .then((dirPath) =%3E %7B%0A      t.context.tempDir = dirPath%0A    %7D)
 %0A%7D)%0A
@@ -503,37 +503,41 @@
 const cwd = 
-process.cwd()
+t.context.tempDir
 %0A  return li
@@ -555,38 +555,70 @@
 d %7D)%0A    .then((
-) =%3E %7B
+result) =%3E %7B%0A      console.log(result)
 %0A      t.fail()%0A
@@ -1402,39 +1402,32 @@
 %0A    %7D)%0A%7D)%0A%0Atest
-.serial
 ('write invalid 
@@ -1475,37 +1475,41 @@
 const cwd = 
-process.cwd()
+t.context.tempDir
 %0A  return li
@@ -1739,29 +1739,33 @@
 t cwd = 
-process.cwd()
+t.context.tempDir
 %0A  retur
@@ -2030,29 +2030,33 @@
 th.join(
-process.cwd()
+t.context.tempDir
 , 'test'
@@ -2087,21 +2087,25 @@
 wd: 
-process.cwd()
+t.context.tempDir
 , sc
@@ -2235,32 +2235,51 @@
 %3E lib.read(%7B cwd
+: t.context.tempDir
  %7D))%0A    .then((

fix scope tests to use temporary files