Skip to content

Commit

Permalink
Merge pull request #190 from nmigueles/init/create-files-inside-folder
Browse files Browse the repository at this point in the history
Folder in init `wollok init [folder]`
  • Loading branch information
PalumboN authored Sep 22, 2024
2 parents fdf4f5c + d5c405a commit f1dd306
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 46 deletions.
6 changes: 4 additions & 2 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export type Options = {
game: boolean,
}

export default function ({ project, name, noTest = false, noCI = false, game = false }: Options): void {
export default function (folder: string | undefined, { project: _project, name, noTest = false, noCI = false, game = false }: Options): void {
const project = join(_project, folder ?? '')

// Initialization
if (existsSync(join(project, 'package.json'))) {
logger.info(yellow(bold(`🚨 There is already a project inside ${project} folder`)))
Expand Down Expand Up @@ -97,7 +99,7 @@ describe "group of tests for pepita" {
}`

const gameDefinition = (exampleName: string) => `import wollok.game.*
const gameDefinition = (exampleName: string) => `import wollok.game.*
import ${exampleName}.pepita
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import run from './commands/run'
import test from './commands/test'
import init from './commands/init'
import logger from 'loglevel'
import { version } from '../package.json'
import { version } from '../package.json'
import { cyan } from 'chalk'

const program = new Command()
.name('wollok')
.description('Wollok Language command line interpreter tool')
.version(cyan(version))
.hook('preAction', (thisCommand, actionCommand) => {
.hook('preAction', (thisCommand, actionCommand) => {
actionCommand.opts().verbose ? logger.setLevel('DEBUG') : logger.setLevel('INFO')
})

Expand Down Expand Up @@ -55,6 +55,7 @@ program.command('repl')

program.command('init')
.description('Create a new Wollok project')
.argument('[folder]', 'folder name, if not provided, the current folder will be used')
.option('-p, --project [filter]', 'path to project', process.cwd())
.option('-n, --name [name]', 'name of the example', undefined)
.option('-g, --game', 'adds a game program to the project', false)
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type FileContent = {

export const createFolderIfNotExists = (folder: string): void => {
if (!existsSync(folder)) {
mkdirSync(folder)
mkdirSync(folder, { recursive: true })
}
}

Expand Down Expand Up @@ -67,10 +67,10 @@ export const validateEnvironment = (environment: Environment, skipValidations: b
try {
const problems = validate(environment)
problems.forEach(problem => logger.info(problemDescription(problem)))
if(!problems.length) {
if (!problems.length) {
logger.info(successDescription('No problems found building the environment!'))
}
else if(problems.some(_ => _.level === 'error')) {
else if (problems.some(_ => _.level === 'error')) {
throw new Error('Aborting run due to validation errors!')
}
} catch (error: any) {
Expand Down
32 changes: 24 additions & 8 deletions test/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { ElementDefinition } from 'cytoscape'
import { existsSync } from 'fs'

type ElementDefinitionQuery = Partial<ElementDefinition['data']>

declare global {
export namespace Chai {
interface Assertion { // TODO: split into the separate modules
connect: (label: string, sourceLabel: string, targetLabel: string, width?: number, style?: string ) => Assertion
}
export namespace Chai {
interface Assertion { // TODO: split into the separate modules
connect: (label: string, sourceLabel: string, targetLabel: string, width?: number, style?: string) => Assertion
pathExists(path: string): Assertion
}

interface Include {
nodeWith: (query: ElementDefinitionQuery) => Assertion
}
interface Include {
nodeWith: (query: ElementDefinitionQuery) => Assertion
}
}
}

export const pathAssertions: Chai.ChaiPlugin = (chai) => {
const { Assertion } = chai

Assertion.addMethod('pathExists', function (path) {
const exists = existsSync(path)
this.assert(
exists,
'expected path #{this} to exist',
'expected path #{this} to not exist',
path
)
})
}

export const diagramAssertions: Chai.ChaiPlugin = (chai) => {
Expand All @@ -34,7 +50,7 @@ export const diagramAssertions: Chai.ChaiPlugin = (chai) => {
new Assertion(diagram).to.deep.contain(query)
})

Assertion.addMethod('connect', function ( label: string, source: string, target: string, width = 1, style = 'solid') {
Assertion.addMethod('connect', function (label: string, source: string, target: string, width = 1, style = 'solid') {
const { data: { id: sourceId } } = this._obj.find(({ data }: any) => data.label === source)
const { data: { id: targetId } } = this._obj.find(({ data }: any) => data.label === target)
const connection = {
Expand Down
78 changes: 47 additions & 31 deletions test/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import chai from 'chai'
import chai, { use } from 'chai'
import { join } from 'path'
import { existsSync, readFileSync, rmSync } from 'fs'
import { readFileSync, rmSync } from 'fs'
import sinon from 'sinon'
import init, { Options } from '../src/commands/init'
import test from '../src/commands/test'
import { pathAssertions } from './assertions'

chai.should()

const expect = chai.expect
use(pathAssertions)

const project = join('examples', 'init-examples', 'basic-example')
const customFolderName = 'custom-folder'
const customFolderProject = join(project, customFolderName)
const GITHUB_FOLDER = join('.github', 'workflows')

const baseOptions: Options = {
Expand All @@ -28,19 +33,20 @@ describe('testing init', () => {

afterEach(() => {
rmSync(project, { recursive: true, force: true })
rmSync(customFolderProject, { recursive: true, force: true })
sinon.restore()
})

it('should create files successfully for default values: ci, no game & example name', async () => {
init(baseOptions)

expect(existsSync(join(project, 'example.wlk'))).to.be.true
expect(existsSync(join(project, 'testExample.wtest'))).to.be.true
expect(existsSync(join(project, 'package.json'))).to.be.true
expect(existsSync(join(project, GITHUB_FOLDER, 'ci.yml'))).to.be.true
expect(existsSync(join(project, 'README.md'))).to.be.true
expect(existsSync(join(project, '.gitignore'))).to.be.true
expect(existsSync(join(project, 'mainExample.wpgm'))).to.be.false
init(undefined, baseOptions)

expect(join(project, 'example.wlk')).to.pathExists
expect(join(project, 'testExample.wtest')).to.pathExists
expect(join(project, 'package.json')).to.pathExists
expect(join(project, GITHUB_FOLDER, 'ci.yml')).to.pathExists
expect(join(project, 'README.md')).to.pathExists
expect(join(project, '.gitignore')).to.pathExists
expect(join(project, 'mainExample.wpgm')).to.pathExists
expect(getResourceFolder()).to.be.undefined

await test(undefined, {
Expand All @@ -54,53 +60,63 @@ describe('testing init', () => {
})

it('should create files successfully for game project with ci & custom example name', () => {
init({
init(undefined, {
...baseOptions,
game: true,
name: 'pepita',
})

expect(existsSync(join(project, 'pepita.wlk'))).to.be.true
expect(existsSync(join(project, 'testPepita.wtest'))).to.be.true
expect(existsSync(join(project, 'mainPepita.wpgm'))).to.be.true
expect(existsSync(join(project, 'package.json'))).to.be.true
expect(existsSync(join(project, GITHUB_FOLDER, 'ci.yml'))).to.be.true
expect(existsSync(join(project, '.gitignore'))).to.be.true
expect(existsSync(join(project, 'README.md'))).to.be.true
expect(join(project, 'pepita.wlk')).to.pathExists
expect(join(project, 'testPepita.wtest')).to.pathExists
expect(join(project, 'mainPepita.wpgm')).to.pathExists
expect(join(project, 'package.json')).to.pathExists
expect(join(project, GITHUB_FOLDER, 'ci.yml')).to.pathExists
expect(join(project, 'README.md')).to.pathExists
expect(join(project, '.gitignore')).to.pathExists
expect(getResourceFolder()).to.be.equal('assets')
})

it('should create files successfully for game project with no ci & no test custom example name', async () => {
init({
init(undefined, {
...baseOptions,
noCI: true,
noTest: true,
name: 'pepita',
})

expect(existsSync(join(project, 'pepita.wlk'))).to.be.true
expect(existsSync(join(project, 'testPepita.wtest'))).to.be.false
expect(existsSync(join(project, 'package.json'))).to.be.true
expect(existsSync(join(project, 'mainPepita.wpgm'))).to.be.false
expect(existsSync(join(project, GITHUB_FOLDER, 'ci.yml'))).to.be.false
expect(existsSync(join(project, '.gitignore'))).to.be.true
expect(existsSync(join(project, 'README.md'))).to.be.true
expect(join(project, 'pepita.wlk')).to.pathExists
expect(join(project, 'testPepita.wtest')).to.pathExists
expect(join(project, 'package.json')).to.pathExists
expect(join(project, 'mainPepita.wpgm')).to.pathExists
expect(join(project, GITHUB_FOLDER, 'ci.yml')).to.pathExists
expect(join(project, '.gitignore')).to.pathExists
expect(join(project, 'README.md')).to.pathExists
})

it('should create files successfully with an argument for the folder name working in combination with project option', async () => {
init(customFolderName, baseOptions)

expect(join(customFolderProject, 'pepita.wlk')).to.pathExists
expect(join(customFolderProject, 'testPepita.wtest')).to.pathExists
expect(join(customFolderProject, 'mainPepita.wpgm')).to.pathExists
expect(join(customFolderProject, 'package.json')).to.pathExists
expect(join(customFolderProject, GITHUB_FOLDER, 'ci.yml')).to.pathExists
expect(join(customFolderProject, 'README.md')).to.pathExists
expect(join(customFolderProject, '.gitignore')).to.pathExists
})

it('should exit with code 1 if folder already exists', () => {
init({
init(undefined, {
...baseOptions,
project: join('examples', 'init-examples', 'existing-folder'),
})

expect(processExitSpy.calledWith(1)).to.be.true
})


})

const getResourceFolder = () => {
const packageJson = readFileSync(join(project, 'package.json'), 'utf8')
const packageJson = readFileSync(join(project, 'package.json'), 'utf8')
const { resourceFolder } = JSON.parse(packageJson)
return resourceFolder
}

0 comments on commit f1dd306

Please sign in to comment.