Skip to content

Commit 2081808

Browse files
authored
merge: PR #34 from chore/18-unit-testing/24-test-lib-path-utils-staging
24 - Test lib path-utils
2 parents aab4ca3 + bfee9eb commit 2081808

File tree

9 files changed

+312
-345
lines changed

9 files changed

+312
-345
lines changed

libs/path-utils/src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,54 @@ pub fn escape_path(path: &str) -> String {
5252
pub fn escape_path(path: &str) -> String {
5353
path.to_string()
5454
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
fn test_normalize_path() {
62+
let path = "/c:/Users/username/Documents";
63+
assert_eq!(normalize_path(path), "c:/Users/username/Documents");
64+
}
65+
66+
#[test]
67+
fn test_normalize_path_windows() {
68+
let path = "/c%3A/Users/username/Documents";
69+
assert_eq!(normalize_path(path), "c:/Users/username/Documents");
70+
}
71+
72+
#[test]
73+
fn test_join_path() {
74+
let base_path = "C:/Users/username/Documents";
75+
let file = "file.sol";
76+
assert_eq!(
77+
join_path(base_path, file),
78+
"C:/Users/username/Documents/file.sol"
79+
);
80+
}
81+
82+
#[test]
83+
fn test_slashify_path() {
84+
let path = "C:\\Users\\username\\Documents";
85+
assert_eq!(slashify_path(path), "C:/Users/username/Documents");
86+
}
87+
88+
#[test]
89+
fn test_slashify_path_double_slash() {
90+
let path = "C:\\Users\\\\username\\Documents";
91+
assert_eq!(slashify_path(path), "C:/Users/username/Documents");
92+
}
93+
94+
#[test]
95+
fn test_escape_path() {
96+
let path = "c://Users/username/Documents";
97+
assert_eq!(escape_path(path), "/c%3A/Users/username/Documents");
98+
}
99+
100+
#[test]
101+
fn test_escape_path_windows() {
102+
let path = "c://Users/username/Documents";
103+
assert_eq!(escape_path(path), "/c%3A/Users/username/Documents");
104+
}
105+
}

vscode/src/code-actions.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import * as path from "path";
2-
import * as os from "os";
3-
import { workspace, ExtensionContext, Uri } from "vscode";
1+
import * as path from 'path';
2+
import * as os from 'os';
3+
import { workspace, ExtensionContext, Uri } from 'vscode';
44
import {
55
LanguageClient,
66
LanguageClientOptions,
77
ServerOptions,
88
TransportKind,
99
SocketTransport,
1010
StreamInfo,
11-
} from "vscode-languageclient/node";
12-
import { TextDecoder } from "util";
13-
import * as net from "net";
11+
} from 'vscode-languageclient/node';
12+
import { TextDecoder } from 'util';
13+
import * as net from 'net';
1414

15-
export async function createCodeActionsClient(
16-
context: ExtensionContext,
17-
): Promise<LanguageClient> {
15+
export async function createCodeActionsClient(context: ExtensionContext): Promise<LanguageClient> {
1816
/*
1917
let connectionInfo = {
2018
port: 9001,
@@ -39,12 +37,7 @@ export async function createCodeActionsClient(
3937

4038
// The server is implemented in node
4139
const serverBinary = context.asAbsolutePath(
42-
path.join(
43-
"dist",
44-
os.platform().startsWith("win")
45-
? "code-actions-server.exe"
46-
: "code-actions-server",
47-
),
40+
path.join('dist', os.platform().startsWith('win') ? 'code-actions-server.exe' : 'code-actions-server'),
4841
);
4942

5043
const serverOptions: ServerOptions = {
@@ -58,17 +51,17 @@ export async function createCodeActionsClient(
5851
// Options to control the language client
5952
const clientOptions: LanguageClientOptions = {
6053
// Register the server for plain text documents
61-
documentSelector: [{ scheme: "file", language: "solidity" }],
54+
documentSelector: [{ scheme: 'file', language: 'solidity' }],
6255
synchronize: {
6356
// Notify the server about file changes to '.clientrc files contained in the workspace
64-
fileEvents: workspace.createFileSystemWatcher("**/.solidhunter.json"),
57+
fileEvents: workspace.createFileSystemWatcher('**/.solidhunter.json'),
6558
},
6659
};
6760

6861
// Create the language client and start the client.
6962
const client = new LanguageClient(
70-
"osmium-solidity-code-actions",
71-
"Osmium Solidity Code Actions Language Server",
63+
'osmium-solidity-code-actions',
64+
'Osmium Solidity Code Actions Language Server',
7265
serverOptions,
7366
clientOptions,
7467
);

vscode/src/fmt-wrapper.ts

Lines changed: 79 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type ForgeFmtResult = {
2727

2828
function isFmtInstalled(): boolean {
2929
try {
30-
exec("forge fmt --version", (error, _stdout, _stderr) => {
30+
exec('forge fmt --version', (error, _stdout, _stderr) => {
3131
if (error) {
3232
throw error;
3333
}
@@ -38,35 +38,30 @@ function isFmtInstalled(): boolean {
3838
}
3939
}
4040

41-
function forgeFmt(
42-
args: ForgeFmtArgs,
43-
debug?: boolean,
44-
): Promise<ForgeFmtResult> {
41+
function forgeFmt(args: ForgeFmtArgs, debug?: boolean): Promise<ForgeFmtResult> {
4542
const { options, files } = args;
4643
const { root, check, raw } = options;
4744

48-
const commandArgs = ["fmt"];
45+
const commandArgs = ['fmt'];
4946

5047
if (root) {
51-
commandArgs.push("--root", `"${root}"`);
48+
commandArgs.push('--root', `"${root}"`);
5249
}
5350

5451
if (check) {
55-
commandArgs.push("--check");
52+
commandArgs.push('--check');
5653
}
5754

5855
if (raw) {
59-
commandArgs.push("--raw");
56+
commandArgs.push('--raw');
6057
}
6158

62-
commandArgs.push(
63-
...files.map((file) => (file.includes(" ") ? `"${file}"` : file)),
64-
);
59+
commandArgs.push(...files.map((file) => (file.includes(' ') ? `"${file}"` : file)));
6560

66-
const command = `forge ${commandArgs.join(" ")}`;
61+
const command = `forge ${commandArgs.join(' ')}`;
6762

6863
if (debug) {
69-
console.debug("command =>", command);
64+
console.debug('command =>', command);
7065
}
7166

7267
return new Promise((resolve, reject) => {
@@ -159,13 +154,6 @@ function registerForgeFmtLinter(context: vscode.ExtensionContext): {fileDisposab
159154
return;
160155
}
161156

162-
if (!vscode.workspace.workspaceFolders?.[0]) {
163-
vscode.window.showErrorMessage(
164-
"Unable to find workspace root. Please open a folder and try again.",
165-
);
166-
return;
167-
}
168-
169157
const options: ForgeFmtOptions = {
170158
root: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
171159
check: false,
@@ -174,68 +162,97 @@ function registerForgeFmtLinter(context: vscode.ExtensionContext): {fileDisposab
174162

175163
const args: ForgeFmtArgs = {
176164
options,
177-
files: [vscode.workspace.workspaceFolders?.[0].uri.fsPath],
165+
files: [document.fileName],
178166
};
179167

180168
forgeFmt(args)
181169
.then((result) => {
182170
if (result.exitCode === 0) {
183-
vscode.window.showInformationMessage("Forge fmt ran successfully.");
171+
vscode.window.showInformationMessage('Forge fmt ran successfully.');
184172
} else {
185-
vscode.window.showErrorMessage(
186-
"Forge fmt failed. Please check the output for details.",
187-
);
173+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
188174

189175
console.log(result.output);
190176
}
191177
})
192178
.catch((error) => {
193-
vscode.window.showErrorMessage(
194-
"Forge fmt failed. Please check the output for details.",
195-
);
179+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
196180
console.error(error);
197181
});
198-
},
199-
);
182+
} else {
183+
vscode.window.showErrorMessage('Forge fmt is only available for solidity files.');
184+
}
185+
});
200186

201-
const formatter = vscode.languages.registerDocumentFormattingEditProvider(
202-
"solidity",
203-
{
204-
provideDocumentFormattingEdits: (document) => {
205-
if (!isFmtInstalled()) {
206-
vscode.window.showErrorMessage(
207-
"Forge fmt is not installed. Please install it and try again.",
208-
);
209-
return;
187+
const lintSolWorkspace = vscode.commands.registerCommand('osmium.format-sol-workspace', function () {
188+
if (!isFmtInstalled()) {
189+
vscode.window.showErrorMessage('Forge fmt is not installed. Please install it and try again.');
190+
return;
191+
}
192+
193+
if (!vscode.workspace.workspaceFolders?.[0]) {
194+
vscode.window.showErrorMessage('Unable to find workspace root. Please open a folder and try again.');
195+
return;
196+
}
197+
198+
const options: ForgeFmtOptions = {
199+
root: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
200+
check: false,
201+
raw: false,
202+
};
203+
204+
const args: ForgeFmtArgs = {
205+
options,
206+
files: [vscode.workspace.workspaceFolders?.[0].uri.fsPath],
207+
};
208+
209+
forgeFmt(args)
210+
.then((result) => {
211+
if (result.exitCode === 0) {
212+
vscode.window.showInformationMessage('Forge fmt ran successfully.');
213+
} else {
214+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
215+
216+
console.log(result.output);
210217
}
218+
})
219+
.catch((error) => {
220+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
221+
console.error(error);
222+
});
223+
});
211224

212-
const options: ForgeFmtOptions = {
213-
root: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
214-
check: false,
215-
raw: false,
216-
};
225+
const formatter = vscode.languages.registerDocumentFormattingEditProvider('solidity', {
226+
provideDocumentFormattingEdits: (document) => {
227+
if (!isFmtInstalled()) {
228+
vscode.window.showErrorMessage('Forge fmt is not installed. Please install it and try again.');
229+
return;
230+
}
217231

218-
const args: ForgeFmtArgs = {
219-
options,
220-
files: [document.fileName],
221-
};
232+
const options: ForgeFmtOptions = {
233+
root: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
234+
check: false,
235+
raw: false,
236+
};
222237

223-
return forgeFmt(args).then((result) => {
224-
if (result.exitCode === 0) {
225-
vscode.window.showInformationMessage("Forge fmt ran successfully.");
226-
} else {
227-
vscode.window.showErrorMessage(
228-
"Forge fmt failed. Please check the output for details.",
229-
);
238+
const args: ForgeFmtArgs = {
239+
options,
240+
files: [document.fileName],
241+
};
230242

231-
console.log(result.output);
232-
}
243+
return forgeFmt(args).then((result) => {
244+
if (result.exitCode === 0) {
245+
vscode.window.showInformationMessage('Forge fmt ran successfully.');
246+
} else {
247+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
233248

234-
return [];
235-
});
236-
},
249+
console.log(result.output);
250+
}
251+
252+
return [];
253+
});
237254
},
238-
);
255+
});
239256

240257
context.subscriptions.push(lintSolFile);
241258
context.subscriptions.push(lintSolWorkspace);

vscode/src/foundry-compiler.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
import * as path from "path";
2-
import { workspace, ExtensionContext } from "vscode";
3-
import {
4-
LanguageClient,
5-
LanguageClientOptions,
6-
ServerOptions,
7-
TransportKind,
8-
} from "vscode-languageclient/node";
9-
import * as os from "os";
1+
import * as path from 'path';
2+
import { workspace, ExtensionContext } from 'vscode';
3+
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
4+
import * as os from 'os';
105

11-
export function createFoundryCompilerClient(
12-
context: ExtensionContext,
13-
): LanguageClient {
6+
export function createFoundryCompilerClient(context: ExtensionContext): LanguageClient {
147
// The server is implemented in node
158
const serverBinary = context.asAbsolutePath(
16-
path.join(
17-
"dist",
18-
os.platform().startsWith("win")
19-
? "foundry-compiler-server.exe"
20-
: "foundry-compiler-server",
21-
),
9+
path.join('dist', os.platform().startsWith('win') ? 'foundry-compiler-server.exe' : 'foundry-compiler-server'),
2210
);
2311

2412
// If the extension is launched in debug mode then the debug server options are used
@@ -34,17 +22,17 @@ export function createFoundryCompilerClient(
3422
// Options to control the language client
3523
const clientOptions: LanguageClientOptions = {
3624
// Register the server for plain text documents
37-
documentSelector: [{ scheme: "file", language: "solidity" }],
25+
documentSelector: [{ scheme: 'file', language: 'solidity' }],
3826
synchronize: {
3927
// Notify the server about file changes to '.clientrc files contained in the workspace
40-
fileEvents: workspace.createFileSystemWatcher("**/.solidhunter.json"),
28+
fileEvents: workspace.createFileSystemWatcher('**/.solidhunter.json'),
4129
},
4230
};
4331

4432
// Create the language client and start the client.
4533
const client = new LanguageClient(
46-
"osmium-solidity-foundry-compiler",
47-
"Osmium Solidity Foundry Compiler Language Server",
34+
'osmium-solidity-foundry-compiler',
35+
'Osmium Solidity Foundry Compiler Language Server',
4836
serverOptions,
4937
clientOptions,
5038
);

0 commit comments

Comments
 (0)