Skip to content

Commit

Permalink
Merge pull request #28 from thadeu/feat/support-to-multi-root-folders
Browse files Browse the repository at this point in the history
added support to multi-root projects
  • Loading branch information
thadeu authored Sep 15, 2023
2 parents ffa6056 + 3907cec commit bab6fac
Show file tree
Hide file tree
Showing 11 changed files with 534 additions and 331 deletions.
3 changes: 2 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.vscode/**
.vscode-test/**
out/test/**
out/**/*.map
out/spec/**
# out/**/*.map
src/**
.gitignore
tsconfig.json
Expand Down
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"type": "string",
"default": "spec",
"description": "Set an custom suffix files tests Example: \"test\""
},
"vscode-run-rspec-file.integration": {
"type": "string",
"default": "rails",
"description": "Set your app mode. Example: \"rails\" or \"other\""
}
}
}
Expand All @@ -131,19 +136,28 @@
],
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "npx webpack --mode production",
"compile": "npx rspack --mode production --config ./webpack.config.js",
"watch": "npx rspack --watch --mode production --config ./webpack.config.js",
"publish": "npx vsce publish",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile && node ./node_modules/vscode/bin/test"
"test": "npm run compile && node ./node_modules/vscode/bin/test",
"test:unit": "vitest"
},
"devDependencies": {
"@rspack/cli": "^0.3.4",
"@types/lodash.compact": "^3.0.7",
"@types/lodash.get": "^4.4.7",
"@types/lodash.uniq": "^4.5.7",
"@types/mocha": "^2.2.42",
"@types/node": "^20.6.0",
"@types/vscode": "^1.82.0",
"lodash.compact": "^3.0.1",
"lodash.uniq": "^4.5.0",
"ts-loader": "^9.4.4",
"tslint": "^5.20.1",
"typescript": "5",
"vite-tsconfig-paths": "^4.2.1",
"vitest": "^0.34.4",
"vsce": "^2.15.0",
"vscode": "^1.1.37",
"webpack": "^5.88.2",
Expand Down
99 changes: 99 additions & 0 deletions spec/FileObject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { describe, expect, test } from 'vitest'
import FileObject from '../src/FileObject'

describe('#toJSON', () => {
test('with rails app spec file', () => {
let filepath = 'spec/controllers/aliquots_controller_spec.rb'
let object = new FileObject(filepath, { integration: 'rails', folder: 'spec', suffix: 'spec' })

let result = object.toJSON()

expect(object.isLibrary(filepath)).toBe(false)
expect(object.isExpectation(filepath)).toBe(true)

expect(result.name).toBe('controllers/aliquots_controller_spec.rb')
expect(result.ext).toBe('.rb')
expect(result.namespace).toBe('spec')
expect(result.suffix).toBe('spec')

expect(result.isRailsApp).toBe(true)
expect(result.inversePath).toBe('app/controllers/aliquots_controller.rb')
expect(result.specPath).toBe('spec/controllers/aliquots_controller_spec.rb')
})

test('with rails app file', () => {
let filepath = 'app/controllers/aliquots_controller.rb'
let object = new FileObject(filepath, { integration: 'rails', folder: 'spec', suffix: 'spec' })

let result = object.toJSON()

expect(object.isLibrary(filepath)).toBe(false)
expect(object.isExpectation(filepath)).toBe(false)

expect(result.name).toBe('controllers/aliquots_controller.rb')
expect(result.ext).toBe('.rb')
expect(result.namespace).toBe('app')
expect(result.suffix).toBe(undefined)

expect(result.isRailsApp).toBe(true)
expect(result.inversePath).toBe('spec/controllers/aliquots_controller_spec.rb')
expect(result.specPath).toBe('spec/controllers/aliquots_controller_spec.rb')
})

test('with library file', () => {
let filepath = 'lib/send_sms.rb'
let object = new FileObject(filepath, { integration: 'rails', folder: 'spec', suffix: 'spec' })

let result = object.toJSON()

expect(object.isLibrary(filepath)).toBe(true)
expect(object.isExpectation(filepath)).toBe(false)

expect(result.name).toBe('lib/send_sms.rb')
expect(result.ext).toBe('.rb')
expect(result.namespace).toBe('lib')
expect(result.suffix).toBe(undefined)

expect(result.isRailsApp).toBe(true)
expect(result.inversePath).toBe('spec/lib/send_sms_spec.rb')
expect(result.specPath).toBe('spec/lib/send_sms_spec.rb')
})

test('with library spec file', () => {
let filepath = 'spec/lib/send_sms.rb'
let object = new FileObject(filepath, { integration: 'rails', folder: 'spec', suffix: 'spec' })

let result = object.toJSON()

expect(object.isLibrary(filepath)).toBe(true)
expect(object.isExpectation(filepath)).toBe(true)

expect(result.name).toBe('lib/send_sms.rb')
expect(result.ext).toBe('.rb')
expect(result.namespace).toBe('spec')
expect(result.suffix).toBe(undefined)

expect(result.isRailsApp).toBe(true)
expect(result.inversePath).toBe('spec/lib/send_sms_spec.rb')
expect(result.specPath).toBe('spec/lib/send_sms_spec.rb')
})

test('when workspace use spec file', () => {
let filepath = 'test/lib/complete_account_test.rb'

let object = new FileObject(filepath, { integration: 'other', folder: 'test', suffix: 'test' })
let result = object.toJSON()

expect(object.isLibrary(filepath)).toBe(true)
expect(object.isExpectation(filepath)).toBe(true)

expect(result.name).toBe('lib/complete_account_test.rb')
expect(result.ext).toBe('.rb')
expect(result.namespace).toBe('test')
expect(result.suffix).toBe('test')

expect(result.isRailsApp).toBe(false)
expect(result.inversePath).toBe('lib/complete_account.rb')
expect(result.specPath).toBe('test/lib/complete_account_test.rb')
})
})
93 changes: 93 additions & 0 deletions spec/WorkSpace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, expect, test } from 'vitest'
import WorkSpace from '../src/WorkSpace'

const railsConfig = { integration: 'rails', folder: 'spec', suffix: 'spec' }

describe('#toJSON', () => {
test('when workspace is one folder', () => {
let fileUri = '/Users/thadeu/code/opensource-community/todo-bcdd/app/models/todo/item/complete.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(railsConfig)

expect(project.uri).toBe('Users/thadeu/code/opensource-community/todo-bcdd')
expect(project.name).toBe('todo-bcdd')
expect(file.path).toBe('app/models/todo/item/complete.rb')
})

test('when workspace is two folder', () => {
let fileUri = '/Users/thadeu/code/opensource-community/todo-bcdd/app/models/todo/item/complete.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(railsConfig)

expect(project.uri).toBe('Users/thadeu/code/opensource-community/todo-bcdd')
expect(project.name).toBe('todo-bcdd')
expect(file.path).toBe('app/models/todo/item/complete.rb')
})

test('when workspace use spec file', () => {
let fileUri = '/Users/thadeu/code/opensource-community/todo-bcdd/spec/models/todo/item/complete_spec.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(railsConfig)

expect(project.uri).toBe('Users/thadeu/code/opensource-community/todo-bcdd')
expect(project.name).toBe('todo-bcdd')
expect(file.path).toBe('spec/models/todo/item/complete_spec.rb')
})

test('when workspace use spec file', () => {
let fileUri = '/Users/thadeu/code/opensource-community/todo-bcdd/test/models/todo/item/complete_test.rb'

const config = { integration: 'rails', folder: 'test', suffix: 'test' }
const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(config)

expect(project.uri).toBe('Users/thadeu/code/opensource-community/todo-bcdd')
expect(project.name).toBe('todo-bcdd')
expect(file.path).toBe('test/models/todo/item/complete_test.rb')
})

test('when workspace use spec file', () => {
let fileUri = '/Users/thadeu/code/opensource-community/packwerk/test/lib/complete_account_test.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri({ integration: 'other', folder: 'test', suffix: 'test' })

expect(project.uri).toBe('Users/thadeu/code/opensource-community/packwerk')
expect(project.name).toBe('packwerk')
expect(file.path).toBe('test/lib/complete_account_test.rb')
})

test('when workspace use spec file', () => {
let fileUri = '/Users/thadeu/code/opensource-community/packwerk/lib/complete_account.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri({ integration: 'other', folder: 'test', suffix: 'test' })

expect(project.uri).toBe('Users/thadeu/code/opensource-community/packwerk')
expect(project.name).toBe('packwerk')
expect(file.path).toBe('lib/complete_account.rb')
expect(file.specPath).toBe('test/lib/complete_account_test.rb')
})

test('when workspace use spec file', () => {
let fileUri = '/Users/thadeu/code/atendesimples/atendesimples-app/spec/controllers/advanced_configurations_controller_spec.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(railsConfig)

expect(project.uri).toBe('Users/thadeu/code/atendesimples/atendesimples-app')
expect(project.name).toBe('atendesimples-app')
expect(file.path).toBe('spec/controllers/advanced_configurations_controller_spec.rb')
expect(file.specPath).toBe('spec/controllers/advanced_configurations_controller_spec.rb')
})
})
22 changes: 22 additions & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const EXTENSION_NAME = 'vscode-run-rspec-file'
export const SETTINGS_RSPEC_COMMAND_KEY = `${EXTENSION_NAME}.custom-command`
export const SETTINGS_RSPEC_FOLDER = `${EXTENSION_NAME}.folder`
export const SETTINGS_RSPEC_CONTROLLER_FOLDER = `${EXTENSION_NAME}.controller-spec-directory`
export const SETTINGS_SUFFIX_FILE = `${EXTENSION_NAME}.suffix`
export const SETTINGS_INTEGRATION_TYPE = `${EXTENSION_NAME}.integration`

export type SettingsType = {
customCommand?: any
folder?: any
suffix?: any
controllerFolder?: any
integration?: any
}

export const SETTINGS_DEFAULT: SettingsType = {
customCommand: 'bundle exec rspec',
folder: 'spec',
suffix: 'spec',
controllerFolder: 'controllers',
integration: 'rails',
}
83 changes: 83 additions & 0 deletions src/FileObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import path from 'node:path'
import compact from 'lodash.compact'

type FileObjectType = {
namespace?: string
name?: string
suffix?: string
ext?: string
path?: string
inversePath?: string
specPath?: string
isRailsApp?: boolean
}

export default class FileObject {
filepath: string

config: {
suffix: string
folder: string
integration: string
}

constructor(filepath?: string, config?: any) {
this.filepath = filepath
this.config = config || {}
}

static fromRelativeUri(filepath?: string, config?: any): FileObjectType {
return new FileObject(filepath, config).toJSON()
}

isExpectation = (text: string) => /^(spec|test)/.test(text)

isLibrary = (text: string) => /lib/.test(text)

isRailsApp = () => this.config?.integration == 'rails'

toJSON(): FileObjectType {
let parts = compact(this.filepath.split(path.sep))

let namespace = parts[0]
let name = parts.slice(1).join('/')

let ext = name.match(/(?<ext>\.rb)$/)?.groups?.ext
let suffix = name.match(/(\w+_)(?<suffix>spec|test)\.rb$/)?.groups?.suffix
let filepath = [namespace, name].join('/')

if (this.isLibrary(namespace)) {
name = parts.join('/')
}

let result = {
namespace,
name,
suffix,
ext,
path: filepath,
inversePath: '',
specPath: '',
isRailsApp: this.isRailsApp(),
}

if (this.isExpectation(suffix)) {
let nameWithoutSuffix = name.replace(new RegExp(`_${suffix}`), '')

if (this.isLibrary(filepath)) {
result.inversePath = ['lib', nameWithoutSuffix].join('/').replace(/^(lib\/)(lib)/, '$2')
} else {
result.inversePath = ['app', nameWithoutSuffix].join('/').replace(/^(app\/)(app)/, '$2')
}
} else {
let nameByMode = this.isRailsApp() ? name : filepath
let nameWithSuffix = nameByMode.replace('.rb', `_${this.config?.suffix}.rb`)

result.inversePath = compact([this.config?.folder, nameWithSuffix]).join('/')
}

result.specPath = this.isExpectation(suffix) ? result.path : result.inversePath

return result
}
}
Loading

0 comments on commit bab6fac

Please sign in to comment.