<commit_before>'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')<commit_msg>fix scope tests to use temporary files<commit_after>'use strict'

// Node.js built-ins

const path = require('path')

// foreign modules

const pify = require('pify')
const temp = pify(require('temp').track())
const test = require('ava')

// local modules

const lib = require('../lib/scope')
const pkg = require('../package.json')

// this module

test.beforeEach((t) => {
  return temp.mkdir(pkg.name.replace(/\//g, '-') + '-')
    .then((dirPath) => {
      t.context.tempDir = dirPath
    })
})

test.serial('read missing .blinkmrc.json', (t) => {
  const cwd = t.context.tempDir
  return lib.read({ cwd })
    .then((result) => {
      console.log(result)
      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('write invalid scope URL to .blinkmrc.json', (t) => {
  const cwd = t.context.tempDir
  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 = t.context.tempDir
  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(t.context.tempDir, 'test')
  return lib.write({ cwd: t.context.tempDir, scope: 'https://example.com/space' })
    .then(() => lib.write({ cwd, scope: 'https://example.com/abcdef' }))
    .then(() => lib.read({ cwd: t.context.tempDir }))
    .then((scope) => {
      t.is(scope, 'https://example.com/abcdef')
{'fraction_non_alphanumeric': 0.2160337552742616, 'fraction_numerical': 0.0, 'mean_word_length': 2.8233870967741934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 48, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5724571', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1730, 'n_words': 363}