Skip to content

Commit

Permalink
Add project name validation (#1335)
Browse files Browse the repository at this point in the history
* Add project name validation

* added more tests, but they're still failing

* fix bug in change and update tests
  • Loading branch information
kevinvangelder authored and GantMan committed Oct 31, 2018
1 parent 8c62f5b commit 0b8d06f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/commands/new.js
Expand Up @@ -30,6 +30,9 @@ async function command (context) {
// check for numbers-only names
const isNumericOnly = /^\d+$/.test(projectName)

// check for alphanumeric name, beginning with a letter
const isValidName = /^[a-z_][a-z0-9_]+$/i.test(projectName)

// ensure we're in a supported directory
if (isIgniteDirectory(process.cwd())) {
context.print.error('The `ignite new` command cannot be run within an already ignited project.')
Expand Down Expand Up @@ -68,6 +71,12 @@ async function command (context) {
process.exit(exitCodes.PROJECT_NAME)
}

// verify the project name is valid
if (!isValidName) {
print.error(`The project name can only contain alphanumeric characters and underscore, but must not begin with a number.`)
process.exit(exitCodes.PROJECT_NAME)
}

// verify the directory doesn't exist already
if (filesystem.exists(projectName) === 'dir') {
print.error(`Directory ${projectName} already exists.`)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/ignite-new/new.test.js
Expand Up @@ -11,7 +11,7 @@ test.before(t => {
process.chdir(tempDir)
})

test('spins up a min app and performs various checks', async t => {
test('spins up a min app and performs various checks', async (t) => {
await execa(IGNITE, ['new', APP_DIR, '--min', '-b', 'ignite-ir-boilerplate-andross'])
process.chdir(APP_DIR)

Expand All @@ -31,8 +31,8 @@ test('spins up a min app and performs various checks', async t => {
// spork a screen and edit it
await execa(IGNITE, ['spork', 'component.ejs'])
const sporkedFile = 'ignite/Spork/ignite-ir-boilerplate-andross/component.ejs'
t.is(jetpack.inspect(sporkedFile).type, 'file')
await jetpack.write(sporkedFile, 'SPORKED!')
t.is(jetpack.inspect(sporkedFile).type, 'file')
await execa(IGNITE, ['generate', 'component', 'Sporkified'])
t.is(jetpack.read('App/Components/Sporkified.js'), 'SPORKED!')

Expand Down
34 changes: 27 additions & 7 deletions tests/integration/ignite-new/newInvalid.test.js
Expand Up @@ -10,32 +10,52 @@ test.before(t => {
process.chdir(tempDir)
})

test('requires a name', async t => {
test('requires a name', async (t) => {
try {
await execa(IGNITE, ['new'])
t.fail()
const result = await execa(IGNITE, ['new'])
t.fail('Blank name did not return an error.')
} catch (err) {
t.is(err.stdout, 'ignite new <projectName>\n\nProject name is required\n')
t.is(err.code, 5)
}
})

test(`doesn't allow kebab-case`, async t => {
test(`doesn't allow kebab-case`, async (t) => {
try {
await execa(IGNITE, ['new', 'chicken-kebab'])
t.fail()
t.fail('Kebab-case name did not return an error.')
} catch (err) {
t.is(err.stdout, 'Please use camel case for your project name. Ex: ChickenKebab\n')
t.is(err.code, 5)
}
})

test('numeric project name', async t => {
test(`doesn't allow 'ignite'`, async (t) => {
try {
await execa(IGNITE, ['new', 'ignite'])
t.fail('ignite name did not return an error.')
} catch (err) {
t.is(err.stdout, 'Hey...that\'s my name! Please name your project something other than \'ignite\'.\n')
t.is(err.code, 5)
}
})

test('numeric project name', async (t) => {
try {
await execa(IGNITE, ['new', '123456'])
t.fail()
t.fail('Numeric-only name did not return an error.')
} catch (err) {
t.is(err.code, 5)
t.true(contains('Please use at least one non-numeric', err.stdout))
}
})

test('project name starting with a number', async (t) => {
try {
await execa(IGNITE, ['new', '1foo'])
t.fail('Project name starting with a number did not fail.')
} catch (err) {
t.is(err.code, 5)
t.true(contains('The project name can only contain alphanumeric characters and underscore, but must not begin with a number.', err.stdout))
}
})

0 comments on commit 0b8d06f

Please sign in to comment.