Skip to content

Commit b472c2b

Browse files
committed
merge: PR 'chore/18-unit-testing/24-test-lib-path-utils-staging'
2 parents c050de4 + e04888c commit b472c2b

File tree

3 files changed

+137
-33
lines changed

3 files changed

+137
-33
lines changed

libs/path-utils/src/lib.rs

+51
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/fmt-wrapper.ts

+75
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,81 @@ function registerForgeFmtLinter(context: vscode.ExtensionContext): {
168168
});
169169
});
170170

171+
const formatter = vscode.languages.registerDocumentFormattingEditProvider('solidity', {
172+
provideDocumentFormattingEdits: (document) => {
173+
if (!isFmtInstalled()) {
174+
vscode.window.showErrorMessage('Forge fmt is not installed. Please install it and try again.');
175+
return;
176+
}
177+
178+
const options: ForgeFmtOptions = {
179+
root: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
180+
check: false,
181+
raw: false,
182+
};
183+
184+
const args: ForgeFmtArgs = {
185+
options,
186+
files: [document.fileName],
187+
};
188+
189+
forgeFmt(args)
190+
.then((result) => {
191+
if (result.exitCode === 0) {
192+
vscode.window.showInformationMessage('Forge fmt ran successfully.');
193+
} else {
194+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
195+
196+
console.log(result.output);
197+
}
198+
})
199+
.catch((error) => {
200+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
201+
console.error(error);
202+
});
203+
} else {
204+
vscode.window.showErrorMessage('Forge fmt is only available for solidity files.');
205+
}
206+
});
207+
208+
const lintSolWorkspace = vscode.commands.registerCommand('osmium.format-sol-workspace', function () {
209+
if (!isFmtInstalled()) {
210+
vscode.window.showErrorMessage('Forge fmt is not installed. Please install it and try again.');
211+
return;
212+
}
213+
214+
if (!vscode.workspace.workspaceFolders?.[0]) {
215+
vscode.window.showErrorMessage('Unable to find workspace root. Please open a folder and try again.');
216+
return;
217+
}
218+
219+
const options: ForgeFmtOptions = {
220+
root: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
221+
check: false,
222+
raw: false,
223+
};
224+
225+
const args: ForgeFmtArgs = {
226+
options,
227+
files: [vscode.workspace.workspaceFolders?.[0].uri.fsPath],
228+
};
229+
230+
forgeFmt(args)
231+
.then((result) => {
232+
if (result.exitCode === 0) {
233+
vscode.window.showInformationMessage('Forge fmt ran successfully.');
234+
} else {
235+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
236+
237+
console.log(result.output);
238+
}
239+
})
240+
.catch((error) => {
241+
vscode.window.showErrorMessage('Forge fmt failed. Please check the output for details.');
242+
console.error(error);
243+
});
244+
});
245+
171246
const formatter = vscode.languages.registerDocumentFormattingEditProvider('solidity', {
172247
provideDocumentFormattingEdits: (document) => {
173248
if (!isFmtInstalled()) {

vscode/src/sidebar-provider.ts

+11-33
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
3131
constructor(private readonly _extensionUri: vscode.Uri) {}
3232

3333
async _osmiumWatcherCallback(uri: vscode.Uri) {
34-
if (!this._view) {
35-
return;
36-
}
34+
if (!this._view) {return;}
3735
const basename = path.basename(uri.fsPath, '.json');
3836
if (basename === 'contracts') {
3937
this._interactContractRepository?.load();
@@ -202,15 +200,9 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
202200
walletPk: 'Enter private key',
203201
walletRpc: 'Enter rpc',
204202
});
205-
if (!inputs) {
206-
return;
207-
}
208-
if (!inputs.walletAddress.startsWith('0x') || !inputs.walletPk.startsWith('0x')) {
209-
return;
210-
}
211-
if (!inputs.walletRpc.startsWith('http') && !inputs.walletRpc.startsWith('ws')) {
212-
return;
213-
}
203+
if (!inputs) {return;}
204+
if (!inputs.walletAddress.startsWith('0x') || !inputs.walletPk.startsWith('0x')) {return;}
205+
if (!inputs.walletRpc.startsWith('http') && !inputs.walletRpc.startsWith('ws')) {return;}
214206

215207
this._walletRepository.createWallet(
216208
inputs.walletName,
@@ -228,9 +220,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
228220
ignoreFocusOut: true,
229221
},
230222
);
231-
if (!walletName) {
232-
return;
233-
}
223+
if (!walletName) {return;}
234224
this._walletRepository.deleteWallet(walletName);
235225
}
236226
break;
@@ -248,12 +238,8 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
248238
contractRpc: 'Enter rpc',
249239
contractChainId: 'Enter chain id',
250240
});
251-
if (!inputs || !inputs.contractAddress.startsWith('0x')) {
252-
return;
253-
}
254-
if (!inputs.contractRpc.startsWith('http') && !inputs.contractRpc.startsWith('ws')) {
255-
return;
256-
}
241+
if (!inputs || !inputs.contractAddress.startsWith('0x')) {return;}
242+
if (!inputs.contractRpc.startsWith('http') && !inputs.contractRpc.startsWith('ws')) {return;}
257243
this._interactContractRepository.createContract(
258244
<Address>inputs['contractAddress'],
259245
JSON.parse(inputs['contractAbi']),
@@ -270,9 +256,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
270256
ignoreFocusOut: true,
271257
},
272258
);
273-
if (!contractName) {
274-
return;
275-
}
259+
if (!contractName) {return;}
276260
this._interactContractRepository.deleteContract(contractName);
277261
}
278262
break;
@@ -286,12 +270,8 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
286270
environmentName: 'Enter name',
287271
environmentRpc: 'Enter rpc',
288272
});
289-
if (!inputs) {
290-
return;
291-
}
292-
if (!inputs.environmentRpc.startsWith('http') && !inputs.environmentRpc.startsWith('ws')) {
293-
return;
294-
}
273+
if (!inputs) {return;}
274+
if (!inputs.environmentRpc.startsWith('http') && !inputs.environmentRpc.startsWith('ws')) {return;}
295275

296276
this._environmentRepository.createEnvironment(inputs.environmentName, <RpcUrl>inputs.environmentRpc);
297277
}
@@ -303,9 +283,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
303283
ignoreFocusOut: true,
304284
},
305285
);
306-
if (!environmentName) {
307-
return;
308-
}
286+
if (!environmentName) {return;}
309287
this._environmentRepository.deleteEnvironment(environmentName);
310288
}
311289
break;

0 commit comments

Comments
 (0)