Skip to content

Commit

Permalink
move tests from src to fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Nov 18, 2024
1 parent e71de2f commit cd2cb3a
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 50 deletions.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions packages/renoun/fixtures/project/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function createServer() {
return {}
}
Empty file.
7 changes: 7 additions & 0 deletions packages/renoun/fixtures/utils/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function basename(path: string, extension: string = ''): string {
const base = path.substring(path.lastIndexOf('/') + 1)
if (extension && base.endsWith(extension)) {
return base.slice(0, -extension.length)
}
return base
}
3 changes: 3 additions & 0 deletions packages/renoun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"renoun": "./dist/cli/index.js"
},
"type": "module",
"imports": {
"#fixtures/*": "./fixtures/*"
},
"exports": {
"./assets": {
"types": "./dist/assets/index.d.ts",
Expand Down
96 changes: 46 additions & 50 deletions packages/renoun/src/file-system/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, test, expect, expectTypeOf } from 'vitest'
import type { MDXContent } from '@renoun/mdx'
import { runInNewContext } from 'node:vm'

import { VirtualFileSystem } from './VirtualFileSystem'
Expand All @@ -16,19 +15,19 @@ import {
describe('file system', () => {
test('virtual file system', async () => {
const fileSystem = new VirtualFileSystem({
'src/project/server.ts': '',
'src/project/types.ts': '',
'fixtures/project/server.ts': '',
'fixtures/project/types.ts': '',
})
const srcDirectory = new Directory({
path: 'src',
const fixturesDirectory = new Directory({
path: 'fixtures',
fileSystem,
})
const directory = await srcDirectory.getDirectory('project')
const directory = await fixturesDirectory.getDirectory('project')

expect(directory).toBeInstanceOf(Directory)
expect(directory?.getName()).toBe('project')

const file = await srcDirectory.getFile('project/server', 'ts')
const file = await fixturesDirectory.getFile('project/server', 'ts')

expect(file).toBeInstanceOf(File)
expect(file?.getName()).toBe('server')
Expand All @@ -43,22 +42,20 @@ describe('file system', () => {
})

test('recursive entries', async () => {
const directory = new Directory({ path: 'src/project' })
const directory = new Directory({ path: 'fixtures/project' })
const entries = await directory.getEntries({
recursive: true,
includeIndexAndReadme: true,
})

expect(entries.map((entry) => entry.getPath())).toMatchInlineSnapshot(`
[
"/client",
"/get-project",
"/refresh",
"/rpc",
"/rpc/client",
"/rpc/server",
"/server",
"/types",
"/client",
]
`)
})
Expand Down Expand Up @@ -100,26 +97,30 @@ describe('file system', () => {
})

test('entry', async () => {
const srcDirectory = new Directory({ path: 'src' })
const fixturesDirectory = new Directory({ path: 'fixtures' })

expect(await srcDirectory.getEntry('project')).toBeInstanceOf(Directory)
expect(await fixturesDirectory.getEntry('project')).toBeInstanceOf(
Directory
)
expect(
await (
await srcDirectory.getDirectoryOrThrow('project')
await fixturesDirectory.getDirectoryOrThrow('project')
).getEntry('server')
).toBeInstanceOf(File)
})

test('directory', async () => {
const componentsDirectory = new Directory({ path: 'src/components' })
const componentsDirectory = new Directory({ path: 'fixtures/components' })
const directory = await componentsDirectory.getDirectory('CodeBlock')

expect(directory).toBeInstanceOf(Directory)
})

test('nested directory', async () => {
const rootDirectory = new Directory()
const nestedDirectory = await rootDirectory.getDirectory('src/project/rpc')
const nestedDirectory = await rootDirectory.getDirectory(
'fixtures/project/rpc'
)

expect(nestedDirectory).toBeInstanceOf(Directory)
})
Expand All @@ -135,29 +136,36 @@ describe('file system', () => {
test('nested file', async () => {
const rootDirectory = new Directory()
const nestedfile = await rootDirectory.getFile(
'src/project/rpc/server',
'fixtures/project/rpc/server',
'ts'
)

expect(nestedfile).toBeInstanceOf(File)
})

test('index file', async () => {
const srcDirectory = new Directory()
const file = await srcDirectory.getFile(['src', 'components', 'index'])
const fixturesDirectory = new Directory()
const file = await fixturesDirectory.getFile([
'fixtures',
'components',
'index',
])

expect(file).toBeInstanceOf(File)
})

test('readme file', async () => {
const srcDirectory = new Directory()
const file = await srcDirectory.getFile('src/components/README', 'mdx')
const fixturesDirectory = new Directory()
const file = await fixturesDirectory.getFile(
'fixtures/components/README',
'mdx'
)

expect(file).toBeInstanceOf(File)
})

test('javascript file', async () => {
const projectDirectory = new Directory({ path: 'src/project' })
const projectDirectory = new Directory({ path: 'fixtures/project' })
const file = await projectDirectory.getFile('server', 'ts')

expect(file!).toBeInstanceOf(JavaScriptFile)
Expand All @@ -177,7 +185,7 @@ describe('file system', () => {
})

test('all file exports', async () => {
const projectDirectory = new Directory({ path: 'src/project' })
const projectDirectory = new Directory({ path: 'fixtures/project' })
const file = await projectDirectory.getFile('server', 'ts')
const fileExports = await file!.getExports()

Expand Down Expand Up @@ -220,7 +228,7 @@ describe('file system', () => {
const projectDirectory = new Directory<{
ts: { createServer: () => void }
}>({
path: 'src/project',
path: 'fixtures/project',
getModule: (path) => import(`../project/${path}`),
})
const file = await projectDirectory.getFileOrThrow('server', 'ts')
Expand Down Expand Up @@ -301,8 +309,8 @@ describe('file system', () => {

test('getRuntimeValue resolves export runtime value from getModule', async () => {
const fileSystemDirectory = new Directory({
path: 'src/file-system',
getModule: (path) => import(`./${path}`),
path: 'fixtures/utils',
getModule: (path) => import(`#fixtures/utils/${path}`),
})
const file = await fileSystemDirectory.getFileOrThrow('path', 'ts')

Expand All @@ -321,34 +329,34 @@ describe('file system', () => {
})

test('uses first file found when no file extension present', async () => {
const projectDirectory = new Directory({ path: 'src/project' })
const projectDirectory = new Directory({ path: 'fixtures/project' })
const file = await projectDirectory.getFile('server')

expect(file).toBeDefined()
})

test('attempts to load index file when targeting directory path', async () => {
const fileSystem = new VirtualFileSystem({
'src/project/index.ts': 'export const project = 1',
'fixtures/project/index.ts': 'export const project = 1',
})
const rootDirectory = new Directory({ fileSystem })
const file = await rootDirectory.getFile('src/project')
const file = await rootDirectory.getFile('fixtures/project')

expect(file).toBeInstanceOf(File)
})

test('attempts to load readme file when targeting directory path', async () => {
const fileSystem = new VirtualFileSystem({
'src/project/README.mdx': '# Project',
'fixtures/project/README.mdx': '# Project',
})
const projectDirectory = new Directory({ path: 'src', fileSystem })
const projectDirectory = new Directory({ path: 'fixtures', fileSystem })
const file = await projectDirectory.getFile('project')

expect(file).toBeInstanceOf(File)
})

test('generates sibling navigation from file', async () => {
const projectDirectory = new Directory({ path: 'src/project' })
const projectDirectory = new Directory({ path: 'fixtures/project' })
const file = await projectDirectory.getFile('server', 'ts')
const [previousEntry, nextEntry] = await file!.getSiblings()

Expand All @@ -357,11 +365,11 @@ describe('file system', () => {
})

test('generates sibling navigation from directory', async () => {
const projectDirectory = new Directory({ path: 'src/project' })
const projectDirectory = new Directory({ path: 'fixtures/project' })
const directory = await projectDirectory.getDirectory('rpc')
const [previousEntry, nextEntry] = await directory!.getSiblings()

expect(previousEntry?.getName()).toBe('refresh')
expect(previousEntry).toBe(undefined)
expect(nextEntry?.getName()).toBe('server')
})

Expand All @@ -381,7 +389,7 @@ describe('file system', () => {

test('generates tree navigation', async () => {
const projectDirectory = new Directory({
path: 'src/project',
path: 'fixtures/project',
basePath: 'project',
})

Expand Down Expand Up @@ -409,18 +417,6 @@ describe('file system', () => {

expect(tree).toMatchInlineSnapshot(`
[
{
"name": "client",
"path": "/project/client",
},
{
"name": "get-project",
"path": "/project/get-project",
},
{
"name": "refresh",
"path": "/project/refresh",
},
{
"children": [
{
Expand Down Expand Up @@ -448,7 +444,7 @@ describe('file system', () => {
})

test('uses directory name when index or readme file', async () => {
const projectDirectory = new Directory({ path: 'src/components' })
const projectDirectory = new Directory({ path: 'fixtures/components' })
const indexFile = await projectDirectory.getFile('index')
const readmeFile = await projectDirectory.getFile('README')

Expand All @@ -458,7 +454,7 @@ describe('file system', () => {

test('adds basePath to file and directory getPath', async () => {
const projectDirectory = new Directory({
path: 'src/project',
path: 'fixtures/project',
basePath: 'renoun',
})
const file = await projectDirectory.getFileOrThrow('server', 'ts')
Expand All @@ -470,7 +466,7 @@ describe('file system', () => {

test('does not add basePath to getPathSegments', async () => {
const projectDirectory = new Directory({
path: 'src/project',
path: 'fixtures/project',
basePath: 'renoun',
})
const segments = (
Expand Down

0 comments on commit cd2cb3a

Please sign in to comment.