Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

file-helper.test.ts

147 lines4.3 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { fs, vol } from 'memfs'
3​
4import {
5 findFilesRecursively,
6 isDirectory,
7 readFileHelper,
8 relativePath,
9 toCleanPath,
10} from '../src/file-helpers.js'
11​
12vi.mock('node:fs', () => fs)
13vi.mock('node:fs/promises', () => fs.promises)
14​
15beforeEach(() => {
16 vol.reset()
17})
18​
19describe('toCleanPath', () => {
20 it('should strip Unix-style leading slash', () => {
21 expect(toCleanPath('/projects/my-app/src/file.ts', '/projects/my-app')).toBe(
22 'src/file.ts',
23 )
24 })
25​
26 it('should strip Windows-style leading backslash', () => {
27 expect(
28 toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:\\Projects\\my-app'),
29 ).toBe('src/file.ts')
30 })
31​
32 it('should handle paths without leading separator', () => {
33 expect(toCleanPath('/projects/my-app', '/projects/my-app')).toBe('')
34 })
35​
36 it('should handle mixed path separators (forward slash path, backslash base)', () => {
37 expect(
38 toCleanPath('C:/Projects/my-app/src/file.ts', 'C:\\Projects\\my-app'),
39 ).toBe('src/file.ts')
40 })
41​
42 it('should handle mixed path separators (backslash path, forward slash base)', () => {
43 expect(
44 toCleanPath('C:\\Projects\\my-app\\src\\file.ts', 'C:/Projects/my-app'),
45 ).toBe('src/file.ts')
46 })
47​
48 it('should handle missing drive letter in path against Windows base', () => {
49 expect(
50 toCleanPath('/Users/me/my-app/src/file.ts', 'C:\\Users\\me\\my-app'),
51 ).toBe('src/file.ts')
52 })
53})
54​
55describe('relativePath', () => {
56 it('relative path with the same directory and strip extension', () => {
57 expect(relativePath('src/utils.ts', 'src/index.ts', true)).toBe('./index')
58 })
59 it('relative path with the same directory', () => {
60 expect(relativePath('src/utils.ts', 'src/index.ts')).toBe('./index.ts')
61 })
62 it('relative from a subdirectory', () => {
63 expect(relativePath('src/something/utils.ts', 'src/index.ts')).toBe(
64 '../index.ts',
65 )
66 })
67 it('relative to a subdirectory', () => {
68 expect(relativePath('src/utils.ts', 'src/something/index.ts')).toBe(
69 './something/index.ts',
70 )
71 })
72 it('same deep directory', () => {
73 expect(
74 relativePath('src/foo/bar/baz/utils.ts', 'src/foo/bar/baz/index.ts'),
75 ).toBe('./index.ts')
76 })
77 it('up several levels and down several levels', () => {
78 expect(relativePath('src/bar/baz/utils.ts', 'src/foo/bar/index.ts')).toBe(
79 '../../foo/bar/index.ts',
80 )
81 })
82 it('relative path with a different directory', () => {
83 expect(
84 relativePath(
85 'src/routes/__root.tsx.ejs',
86 'src/integrations/tanstack-query/layout.tsx',
87 ),
88 ).toBe('../integrations/tanstack-query/layout.tsx')
89 })
90 it('windows with ./ prefix', () => {
91 expect(
92 relativePath(
93 '.\\src\\main.tsx.ejs',
94 'src/integrations/tanstack-query/root-provider.tsx',
95 ),
96 ).toBe('./integrations/tanstack-query/root-provider.tsx')
97 })
98 it('windows without ./ prefix', () => {
99 expect(
100 relativePath(
101 'src\\routes\\__root.tsx.ejs',
102 'src/integrations/clerk/provider.tsx',
103 true,
104 ),
105 ).toBe('../integrations/clerk/provider')
106 })
107})
108​
109describe('readFileHelper', () => {
110 it('should read a file', async () => {
111 vol.mkdirSync('/src')
112 fs.writeFileSync('/src/test.txt', 'Hello')
113 expect(readFileHelper('/src/test.txt')).toBe('Hello')
114 })
115 it('should read a binary file', async () => {
116 vol.mkdirSync('/src')
117 fs.writeFileSync('/src/test.jpg', 'Hello')
118 expect(readFileHelper('/src/test.jpg')).toBe('base64::SGVsbG8=')
119 })
120})
121​
122describe('isDirectory', () => {
123 it('should check on files and directories', () => {
124 vol.mkdirSync('/src')
125 fs.writeFileSync('/src/test.txt', 'Hello')
126 expect(isDirectory('/src/test.txt')).toBe(false)
127 expect(isDirectory('/src')).toBe(true)
128 })
129})
130​
131describe('findFilesRecursively', () => {
132 it('find files recursively', () => {
133 vol.mkdirSync('/src/subdir/subdir2', { recursive: true })
134 fs.writeFileSync('/src/test.txt', 'Hello')
135 fs.writeFileSync('/src/subdir/test.txt', 'Hello')
136 fs.writeFileSync('/src/subdir/subdir2/test.txt', 'Hello')
137​
138 const files = {}
139 findFilesRecursively('/src', files)
140​
141 expect(Object.keys(files)).toEqual([
142 '/src/subdir/subdir2/test.txt',
143 '/src/subdir/test.txt',
144 '/src/test.txt',
145 ])
146 })
147})