1
1
import * as vscode from "vscode" ;
2
2
import * as fs from "fs" ;
3
+ import * as path from "path" ;
3
4
import generateTest from "./generateTest" ;
4
5
import generateValidation from "./generateValidation" ;
5
6
import { appendToFile } from "./utils" ;
6
7
8
+ function getTestPath ( filePath : string ) : string {
9
+ const dirName = path . dirname ( filePath ) ;
10
+ const testDir = path . join ( dirName , "tests" ) ;
11
+ if ( ! fs . existsSync ( testDir ) ) {
12
+ fs . mkdirSync ( testDir ) ;
13
+ }
14
+ const baseName = path . basename ( filePath ) ;
15
+ return path . join ( testDir , baseName . replace ( ".nf" , ".nf.test" ) ) ;
16
+ }
17
+
7
18
async function createTest ( filePath : string , token : string ) : Promise < boolean > {
8
19
return vscode . window . withProgress (
9
20
{
@@ -15,8 +26,8 @@ async function createTest(filePath: string, token: string): Promise<boolean> {
15
26
try {
16
27
progress . report ( { message : "Reading file contents" } ) ;
17
28
const content = fs . readFileSync ( filePath , "utf8" ) ;
18
- const newFilePath = filePath . replace ( ".nf" , ".nf.test" ) ;
19
- const uri = vscode . Uri . file ( newFilePath ) ;
29
+ const testPath = getTestPath ( filePath ) ;
30
+ const uri = vscode . Uri . file ( testPath ) ;
20
31
21
32
// Create new file
22
33
progress . report ( { message : "Creating test file" } ) ;
@@ -53,7 +64,7 @@ async function createTest(filePath: string, token: string): Promise<boolean> {
53
64
// Save
54
65
await document . save ( ) ;
55
66
56
- vscode . window . showInformationMessage ( `nf-test created: ${ newFilePath } ` ) ;
67
+ vscode . window . showInformationMessage ( `nf-test created: ${ testPath } ` ) ;
57
68
58
69
return true ;
59
70
} catch ( error : any ) {
0 commit comments