fix gentests.js warnings

feature/default_network_editable
brunobar79 7 years ago
parent 73b6d5ff95
commit ce0232fa1e
  1. 132
      gentests.js

@ -1,6 +1,5 @@
const fs = require('fs') const fs = require('fs')
const async = require('async') const async = require('async')
const path = require('path')
const promisify = require('pify') const promisify = require('pify')
// start(/\.selectors.js/, generateSelectorTest).catch(console.error) // start(/\.selectors.js/, generateSelectorTest).catch(console.error)
@ -8,7 +7,6 @@ const promisify = require('pify')
startContainer(/\.container.js/, generateContainerTest).catch(console.error) startContainer(/\.container.js/, generateContainerTest).catch(console.error)
async function getAllFileNames (dirName) { async function getAllFileNames (dirName) {
const rootPath = path.join(__dirname, dirName)
const allNames = (await promisify(fs.readdir)(dirName)) const allNames = (await promisify(fs.readdir)(dirName))
const fileNames = allNames.filter(name => name.match(/^.+\./)) const fileNames = allNames.filter(name => name.match(/^.+\./))
const dirNames = allNames.filter(name => name.match(/^[^.]+$/)) const dirNames = allNames.filter(name => name.match(/^[^.]+$/))
@ -16,7 +14,7 @@ async function getAllFileNames (dirName) {
const fullPathDirNames = dirNames.map(d => `${dirName}/${d}`) const fullPathDirNames = dirNames.map(d => `${dirName}/${d}`)
const subNameArrays = await promisify(async.map)(fullPathDirNames, getAllFileNames) const subNameArrays = await promisify(async.map)(fullPathDirNames, getAllFileNames)
let subNames = [] let subNames = []
subNameArrays.forEach(subNameArray => subNames = [...subNames, ...subNameArray]) subNameArrays.forEach(subNameArray => { subNames = [...subNames, ...subNameArray] })
return [ return [
...fileNames.map(name => dirName + '/' + name), ...fileNames.map(name => dirName + '/' + name),
@ -24,6 +22,7 @@ async function getAllFileNames (dirName) {
] ]
} }
/*
async function start (fileRegEx, testGenerator) { async function start (fileRegEx, testGenerator) {
const fileNames = await getAllFileNames('./ui/app') const fileNames = await getAllFileNames('./ui/app')
const sFiles = fileNames.filter(name => name.match(fileRegEx)) const sFiles = fileNames.filter(name => name.match(fileRegEx))
@ -46,12 +45,12 @@ async function start (fileRegEx, testGenerator) {
}) })
} }
*/
async function startContainer (fileRegEx, testGenerator) { async function startContainer (fileRegEx, testGenerator) {
const fileNames = await getAllFileNames('./ui/app') const fileNames = await getAllFileNames('./ui/app')
const sFiles = fileNames.filter(name => name.match(fileRegEx)) const sFiles = fileNames.filter(name => name.match(fileRegEx))
let sFileMethodNames
async.each(sFiles, async (sFile, cb) => { async.each(sFiles, async (sFile, cb) => {
console.log(`sFile`, sFile) console.log(`sFile`, sFile)
const [, sRootPath, sPath] = sFile.match(/^(.+\/)([^/]+)$/) const [, sRootPath, sPath] = sFile.match(/^(.+\/)([^/]+)$/)
@ -62,64 +61,68 @@ async function startContainer (fileRegEx, testGenerator) {
__dirname + '/' + sFile, __dirname + '/' + sFile,
'utf8', 'utf8',
async (err, result) => { async (err, result) => {
console.log(`result`, result.length) if (err) {
const returnObjectStrings = result console.log('Error: ', err)
.match(/return\s(\{[\s\S]+?})\n}/g) } else {
.map(str => { console.log(`result`, result.length)
return str const returnObjectStrings = result
.slice(0, str.length - 1) .match(/return\s(\{[\s\S]+?})\n}/g)
.slice(7) .map(str => {
.replace(/\n/g, '') return str
.replace(/\s\s+/g, ' ') .slice(0, str.length - 1)
.slice(7)
.replace(/\n/g, '')
.replace(/\s\s+/g, ' ')
})
const mapStateToPropsAssertionObject = returnObjectStrings[0]
.replace(/\w+:\s\w+\([\w,\s]+\),/g, str => {
const strKey = str.match(/^\w+/)[0]
return strKey + ': \'mock' + str.match(/^\w+/)[0].replace(/^./, c => c.toUpperCase()) + ':mockState\',\n'
})
.replace(/{\s\w.+/, firstLinePair => `{\n ${firstLinePair.slice(2)}`)
.replace(/\w+:.+,/g, s => ` ${s}`)
.replace(/}/g, s => ` ${s}`)
let mapDispatchToPropsMethodNames
if (returnObjectStrings[1]) {
mapDispatchToPropsMethodNames = returnObjectStrings[1].match(/\s\w+:\s/g).map(str => str.match(/\w+/)[0])
}
const proxyquireObject = ('{\n ' + result
.match(/import\s{[\s\S]+?}\sfrom\s.+/g)
.map(s => s.replace(/\n/g, ''))
.map((s, i) => {
const proxyKeys = s.match(/{.+}/)[0].match(/\w+/g)
return '\'' + s.match(/'(.+)'/)[1] + '\': { ' + (proxyKeys.length > 1
? '\n ' + proxyKeys.join(': () => {},\n ') + ': () => {},\n '
: proxyKeys[0] + ': () => {},') + ' }'
})
.join(',\n ') + '\n}')
.replace('{ connect: () => {}, },', `{
connect: (ms, md) => {
mapStateToProps = ms
mapDispatchToProps = md
return () => ({})
},
},`)
// console.log(`proxyquireObject`, proxyquireObject);
// console.log(`mapStateToPropsAssertionObject`, mapStateToPropsAssertionObject);
// console.log(`mapDispatchToPropsMethodNames`, mapDispatchToPropsMethodNames);
const containerTest = generateContainerTest(sPath, {
mapStateToPropsAssertionObject,
mapDispatchToPropsMethodNames,
proxyquireObject,
}) })
const mapStateToPropsAssertionObject = returnObjectStrings[0] // console.log(`containerTest`, `${__dirname}/${sRootPath}tests/${testFilePath}`, containerTest);
.replace(/\w+:\s\w+\([\w,\s]+\),/g, str => { console.log('----')
const strKey = str.match(/^\w+/)[0] console.log(`sRootPath`, sRootPath)
return strKey + ': \'mock' + str.match(/^\w+/)[0].replace(/^./, c => c.toUpperCase()) + ':mockState\',\n' console.log(`testFilePath`, testFilePath)
}) await promisify(fs.writeFile)(
.replace(/{\s\w.+/, firstLinePair => `{\n ${firstLinePair.slice(2)}`) `${__dirname}/${sRootPath}tests/${testFilePath}`,
.replace(/\w+:.+,/g, s => ` ${s}`) containerTest,
.replace(/}/g, s => ` ${s}`) 'utf8'
let mapDispatchToPropsMethodNames )
if (returnObjectStrings[1]) {
mapDispatchToPropsMethodNames = returnObjectStrings[1].match(/\s\w+:\s/g).map(str => str.match(/\w+/)[0])
} }
const proxyquireObject = ('{\n ' + result
.match(/import\s{[\s\S]+?}\sfrom\s.+/g)
.map(s => s.replace(/\n/g, ''))
.map((s, i) => {
const proxyKeys = s.match(/{.+}/)[0].match(/\w+/g)
return '\'' + s.match(/'(.+)'/)[1] + '\': { ' + (proxyKeys.length > 1
? '\n ' + proxyKeys.join(': () => {},\n ') + ': () => {},\n '
: proxyKeys[0] + ': () => {},') + ' }'
})
.join(',\n ') + '\n}')
.replace('{ connect: () => {}, },', `{
connect: (ms, md) => {
mapStateToProps = ms
mapDispatchToProps = md
return () => ({})
},
},`)
// console.log(`proxyquireObject`, proxyquireObject);
// console.log(`mapStateToPropsAssertionObject`, mapStateToPropsAssertionObject);
// console.log(`mapDispatchToPropsMethodNames`, mapDispatchToPropsMethodNames);
const containerTest = generateContainerTest(sPath, {
mapStateToPropsAssertionObject,
mapDispatchToPropsMethodNames,
proxyquireObject,
})
// console.log(`containerTest`, `${__dirname}/${sRootPath}tests/${testFilePath}`, containerTest);
console.log('----')
console.log(`sRootPath`, sRootPath)
console.log(`testFilePath`, testFilePath)
await promisify(fs.writeFile)(
`${__dirname}/${sRootPath}tests/${testFilePath}`,
containerTest,
'utf8'
)
} }
) )
}, (err) => { }, (err) => {
@ -127,7 +130,7 @@ async function startContainer (fileRegEx, testGenerator) {
}) })
} }
/*
function generateMethodList (methodArray) { function generateMethodList (methodArray) {
return methodArray.map(n => ' ' + n).join(',\n') + ',' return methodArray.map(n => ' ' + n).join(',\n') + ','
} }
@ -143,7 +146,7 @@ function generateMethodDescribeBlock (methodName, index) {
})` })`
return describeBlock return describeBlock
} }
*/
function generateDispatchMethodDescribeBlock (methodName, index) { function generateDispatchMethodDescribeBlock (methodName, index) {
const describeBlock = const describeBlock =
`${index ? ' ' : ''}describe('${methodName}()', () => { `${index ? ' ' : ''}describe('${methodName}()', () => {
@ -154,12 +157,13 @@ function generateDispatchMethodDescribeBlock (methodName, index) {
})` })`
return describeBlock return describeBlock
} }
/*
function generateMethodDescribeBlocks (methodArray) { function generateMethodDescribeBlocks (methodArray) {
return methodArray return methodArray
.map((methodName, index) => generateMethodDescribeBlock(methodName, index)) .map((methodName, index) => generateMethodDescribeBlock(methodName, index))
.join('\n\n') .join('\n\n')
} }
*/
function generateDispatchMethodDescribeBlocks (methodArray) { function generateDispatchMethodDescribeBlocks (methodArray) {
return methodArray return methodArray
@ -167,6 +171,7 @@ function generateDispatchMethodDescribeBlocks (methodArray) {
.join('\n\n') .join('\n\n')
} }
/*
function generateSelectorTest (name, methodArray) { function generateSelectorTest (name, methodArray) {
return `import assert from 'assert' return `import assert from 'assert'
import { import {
@ -192,6 +197,7 @@ describe('${name.match(/^[^.]+/)} utils', () => {
})` })`
} }
*/
function generateContainerTest (sPath, { function generateContainerTest (sPath, {
mapStateToPropsAssertionObject, mapStateToPropsAssertionObject,

Loading…
Cancel
Save