|
1 | 1 | import { join, relative, resolve } from 'path' |
2 | | -import { ensureDirSync, remove } from 'fs-extra' |
| 2 | +import { appendFileSync, ensureDirSync, ensureFileSync, remove } from 'fs-extra' |
3 | 3 | import { |
4 | 4 | exists, |
5 | 5 | findAbsoluteDvcRootPath, |
6 | 6 | findDvcRootPaths, |
7 | 7 | isDirectory, |
8 | 8 | isSameOrChild, |
9 | | - getModifiedTime |
| 9 | + getModifiedTime, |
| 10 | + findOrCreateDvcYamlFile, |
| 11 | + scriptCommand |
10 | 12 | } from '.' |
11 | 13 | import { dvcDemoPath } from '../test/util' |
12 | 14 | import { DOT_DVC } from '../cli/dvc/constants' |
13 | 15 |
|
14 | 16 | jest.mock('../cli/dvc/reader') |
| 17 | +jest.mock('fs-extra', () => { |
| 18 | + const actualModule = jest.requireActual('fs-extra') |
| 19 | + return { |
| 20 | + __esModule: true, |
| 21 | + ...actualModule, |
| 22 | + appendFileSync: jest.fn(), |
| 23 | + ensureFileSync: jest.fn() |
| 24 | + } |
| 25 | +}) |
| 26 | + |
| 27 | +const mockedAppendFileSync = jest.mocked(appendFileSync) |
| 28 | +const mockedEnsureFileSync = jest.mocked(ensureFileSync) |
15 | 29 |
|
16 | 30 | beforeEach(() => { |
17 | 31 | jest.resetAllMocks() |
@@ -151,3 +165,70 @@ describe('getModifiedTime', () => { |
151 | 165 | expect(epoch).toBeGreaterThan(1640995200000) |
152 | 166 | }) |
153 | 167 | }) |
| 168 | + |
| 169 | +describe('findOrCreateDvcYamlFile', () => { |
| 170 | + it('should make sure a dvc.yaml file exists', () => { |
| 171 | + const cwd = '/cwd' |
| 172 | + findOrCreateDvcYamlFile(cwd, '/my/training/script.py') |
| 173 | + |
| 174 | + expect(mockedEnsureFileSync).toHaveBeenCalledWith(`${cwd}/dvc.yaml`) |
| 175 | + }) |
| 176 | + |
| 177 | + it('should add the training script as a train stage in the dvc.yaml file', () => { |
| 178 | + const cwd = '/cwd' |
| 179 | + findOrCreateDvcYamlFile(cwd, '/my/training/script.py') |
| 180 | + |
| 181 | + expect(mockedAppendFileSync).toHaveBeenCalledWith( |
| 182 | + `${cwd}/dvc.yaml`, |
| 183 | + expect.stringMatching(/^\s+stages:\s+train:/) |
| 184 | + ) |
| 185 | + }) |
| 186 | + |
| 187 | + it('should add the training script as a relative path to the cwd', () => { |
| 188 | + findOrCreateDvcYamlFile( |
| 189 | + '/dir/my_project/', |
| 190 | + '/dir/my_project/src/training/train.py' |
| 191 | + ) |
| 192 | + |
| 193 | + expect(mockedAppendFileSync).toHaveBeenCalledWith( |
| 194 | + expect.anything(), |
| 195 | + expect.stringContaining(join('src', 'training', 'train.py')) |
| 196 | + ) |
| 197 | + |
| 198 | + findOrCreateDvcYamlFile( |
| 199 | + '/dir/my_project/', |
| 200 | + '/dir/my_other_project/train.py' |
| 201 | + ) |
| 202 | + |
| 203 | + expect(mockedAppendFileSync).toHaveBeenCalledWith( |
| 204 | + expect.anything(), |
| 205 | + expect.stringContaining(join('..', 'my_other_project', 'train.py')) |
| 206 | + ) |
| 207 | + }) |
| 208 | + |
| 209 | + it('should use the jupyter nbconvert command if the training script is a Jupyter notebook', () => { |
| 210 | + findOrCreateDvcYamlFile('/', '/train.ipynb') |
| 211 | + |
| 212 | + expect(mockedAppendFileSync).toHaveBeenCalledWith( |
| 213 | + expect.anything(), |
| 214 | + expect.stringContaining(scriptCommand.JUPYTER) |
| 215 | + ) |
| 216 | + expect(mockedAppendFileSync).not.toHaveBeenCalledWith( |
| 217 | + expect.anything(), |
| 218 | + expect.stringContaining(scriptCommand.PYTHON) |
| 219 | + ) |
| 220 | + }) |
| 221 | + |
| 222 | + it('should use the python command if the training script is not a Jupyter notebook', () => { |
| 223 | + findOrCreateDvcYamlFile('/', '/train.py') |
| 224 | + |
| 225 | + expect(mockedAppendFileSync).not.toHaveBeenCalledWith( |
| 226 | + expect.anything(), |
| 227 | + expect.stringContaining(scriptCommand.JUPYTER) |
| 228 | + ) |
| 229 | + expect(mockedAppendFileSync).toHaveBeenCalledWith( |
| 230 | + expect.anything(), |
| 231 | + expect.stringContaining(scriptCommand.PYTHON) |
| 232 | + ) |
| 233 | + }) |
| 234 | +}) |
0 commit comments