<commit_before>// this is a weird meta test.  It verifies that all the instances of
// `npm.config.get(...)` are:
// a) Simple strings, and not variables
// b) Documented
// c) Defined in the `npmconf` package.

var test = require("tap").test
var fs = require("fs")
var path = require("path")
var root = path.resolve(__dirname, "..", "..")
var lib = path.resolve(root, "lib")
var nm = path.resolve(root, "node_modules")
var doc = path.resolve(root, "doc/misc/npm-config.md")
var FILES = []
var CONFS = {}
var DOC = {}

var exceptions = [
  path.resolve(lib, "adduser.js"),
  path.resolve(lib, "config.js"),
  path.resolve(lib, "publish.js"),
  path.resolve(lib, "utils", "lifecycle.js"),
  path.resolve(lib, "utils", "map-to-registry.js"),
  path.resolve(nm, "npm-registry-client", "lib", "publish.js"),
  path.resolve(nm, "npm-registry-client", "lib", "request.js")
]

test("get files", function (t) {
  walk(nm)
  walk(lib)
  t.pass("got files")
  t.end()

  function walk(lib) {
    var files = fs.readdirSync(lib).map(function (f) {
      return path.resolve(lib, f)
    })
    files.forEach(function (f) {
      try {
        var s = fs.statSync(f)
      } catch (er) {
        return
      }
      if (s.isDirectory())
        walk(f)
      else if (f.match(/\.js$/))
        FILES.push(f)
    })
  }
})

test("get lines", function (t) {
  FILES.forEach(function (f) {
    var lines = fs.readFileSync(f, 'utf8').split('\n')
    lines.forEach(function (l, i) {
      var matches = l.split(/conf(?:ig)?\.get\(/g)
      matches.shift()
      matches.forEach(function (m) {
        m = m.split(')').shift()
        var literal = m.match(/^['"].+['"]$/)
        if (literal) {
          m = m.slice(1, -1)
          if (!m.match(/^\_/) && m !== 'argv')
            CONFS[m] = {
              file: f,
              line: i
            }
        } else if (exceptions.indexOf(f) === -1) {
          t.fail("non-string-literal config used in " + f + ":" + i)
        }
      })
    })
  })
  t.pass("got lines")
  t.end()
})

test("get docs", function (t) {
  var d = fs.readFileSync(doc, "utf8").split("\n")
  // walk down until the "## Config Settings" section
  for (var i = 0; i < d.length && d[i] !== "## Config Settings"; i++);
  i++
  // now gather up all the ^###\s lines until the next ^##\s
  var doclines = []
  for (; i < d.length && !d[i].match(/^## /); i++) {
    if (d[i].match(/^### /))
      DOC[ d[i].replace(/^### /, '').trim() ] = true
  }
  t.pass("read the docs")
  t.end()
})

test("check configs", function (t) {
  var defs = require("npmconf/config-defs.js")
  var types = Object.keys(defs.types)<commit_msg>test: make config-meta test work on Windows<commit_after>// this is a weird meta test.  It verifies that all the instances of
// `npm.config.get(...)` are:
// a) Simple strings, and not variables
// b) Documented
// c) Defined in the `npmconf` package.

var test = require("tap").test
var fs = require("fs")
var path = require("path")
var root = path.resolve(__dirname, "..", "..")
var lib = path.resolve(root, "lib")
var nm = path.resolve(root, "node_modules")
var doc = path.resolve(root, "doc/misc/npm-config.md")
var FILES = []
var CONFS = {}
var DOC = {}

var exceptions = [
  path.resolve(lib, "adduser.js"),
  path.resolve(lib, "config.js"),
  path.resolve(lib, "publish.js"),
  path.resolve(lib, "utils", "lifecycle.js"),
  path.resolve(lib, "utils", "map-to-registry.js"),
  path.resolve(nm, "npm-registry-client", "lib", "publish.js"),
  path.resolve(nm, "npm-registry-client", "lib", "request.js")
]

test("get files", function (t) {
  walk(nm)
  walk(lib)
  t.pass("got files")
  t.end()

  function walk(lib) {
    var files = fs.readdirSync(lib).map(function (f) {
      return path.resolve(lib, f)
    })
    files.forEach(function (f) {
      try {
        var s = fs.statSync(f)
      } catch (er) {
        return
      }
      if (s.isDirectory())
        walk(f)
      else if (f.match(/\.js$/))
        FILES.push(f)
    })
  }
})

test("get lines", function (t) {
  FILES.forEach(function (f) {
    var lines = fs.readFileSync(f, 'utf8').split(/\r|\n/)
    lines.forEach(function (l, i) {
      var matches = l.split(/conf(?:ig)?\.get\(/g)
      matches.shift()
      matches.forEach(function (m) {
        m = m.split(')').shift()
        var literal = m.match(/^['"].+['"]$/)
        if (literal) {
          m = m.slice(1, -1)
          if (!m.match(/^\_/) && m !== 'argv')
            CONFS[m] = {
              file: f,
              line: i
            }
        } else if (exceptions.indexOf(f) === -1) {
          t.fail("non-string-literal config used in " + f + ":" + i)
        }
      })
    })
  })
  t.pass("got lines")
  t.end()
})

test("get docs", function (t) {
  var d = fs.readFileSync(doc, "utf8").split(/\r|\n/)
  // walk down until the "## Config Settings" section
  for (var i = 0; i < d.length && d[i] !== "## Config Settings"; i++);
  i++
  // now gather up all the ^###\s lines until the next ^##\s
  var doclines = []
  for (; i < d.length && !d[i].match(/^## /); i++) {
    if (d[i].match(/^### /))
      DOC[ d[i].replace(/^### /, '').trim() ] = true
  }
  t.pass("read the docs")
  t.end()
})

test("check configs", function (t) {
  var defs = require("npmconf/config-defs.js")
  var types = Object.keys(defs.types)
{'fraction_non_alphanumeric': 0.21245283018867925, 'fraction_numerical': 0.0022641509433962265, 'mean_word_length': 2.4466840052015604, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5289922', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1923, 'n_words': 467}