Repository navigation
test: add ftp, sftp and file operation tests (#2778) #2928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+438
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b9ddb9
test: add ftp, sftp and file operation tests (#2778)
Elitex07 8b041f8
Merge branch 'main' into issue-2778
UnschooledGamer 985adfd
chore(lint): sort imports
UnschooledGamer 0aada9c
Merge branch 'main' into issue-2778
Elitex07 27ad3d5
test: fix ftp/sftp/fs runtime test assertions
Elitex07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import fsOperation from "fileSystem"; | ||
| import Url from "utils/Url"; | ||
| import { SkipTest, TestRunner } from "./tester"; | ||
|
|
||
| export async function runFtpTests(writeOutput) { | ||
| const runner = new TestRunner("FTP API Tests"); | ||
|
|
||
| const testDir = window.FTP_TEST_URL; | ||
|
|
||
| runner.test("FTP_TEST_URL is defined", (test) => { | ||
| if (!testDir) { | ||
| throw new SkipTest( | ||
| "window.FTP_TEST_URL is not defined. Set it to run FTP tests.", | ||
| ); | ||
| } | ||
| test.assert(typeof testDir === "string", "FTP_TEST_URL should be a string"); | ||
| test.assert( | ||
| testDir.startsWith("ftp://"), | ||
| "FTP_TEST_URL should start with ftp://", | ||
| ); | ||
| }); | ||
|
|
||
| runner.test("fsOperation returns a FileSystem object for FTP", (test) => { | ||
| if (!testDir) throw new SkipTest("No FTP_TEST_URL"); | ||
| const fs = fsOperation(testDir); | ||
| test.assert(fs !== null, "fsOperation should return filesystem handler"); | ||
| test.assert( | ||
| typeof fs.createFile === "function", | ||
| "createFile should be a function", | ||
| ); | ||
| test.assert(typeof fs.exists === "function", "exists should be a function"); | ||
| }); | ||
|
|
||
| runner.test( | ||
| "FTP: createFile, exists, writeFile, readFile, delete", | ||
| async (test) => { | ||
| if (!testDir) throw new SkipTest("No FTP_TEST_URL"); | ||
|
|
||
| const fs = fsOperation(testDir); | ||
| const filename = `__ftp_test_${Date.now()}__.txt`; | ||
| const fileUrl = Url.join(testDir, filename); | ||
|
|
||
| try { | ||
| // 1. Create the file | ||
| const createdUrl = await fs.createFile(filename, "ftp test content"); | ||
| test.assertEqual( | ||
| // Provider adds default port/query to the URL; compare paths only | ||
| Url.pathname(createdUrl), | ||
| Url.pathname(fileUrl), | ||
| "Created file URL should match expected path", | ||
| ); | ||
|
|
||
| // 2. Check existence | ||
| const fileFs = fsOperation(createdUrl); | ||
| // Native FTP exists() resolves 1/0, not a boolean | ||
| const exists = Boolean(await fileFs.exists()); | ||
| test.assertEqual(exists, true, "Created file should exist on FTP"); | ||
|
|
||
| // 3. Read content | ||
| const content = await fileFs.readFile("utf-8"); | ||
| test.assertEqual( | ||
| content, | ||
| "ftp test content", | ||
| "Read content should match", | ||
| ); | ||
|
|
||
| // 4. Write new content | ||
| await fileFs.writeFile("updated ftp content"); | ||
| const updatedContent = await fileFs.readFile("utf-8"); | ||
| test.assertEqual( | ||
| updatedContent, | ||
| "updated ftp content", | ||
| "Read content should match updated content", | ||
| ); | ||
|
|
||
| // 5. Stat check | ||
| const stat = await fileFs.stat(); | ||
| test.assert(stat !== null, "Stat should not be null"); | ||
| test.assertEqual(stat.isFile, true, "Stat should show isFile true"); | ||
| test.assertEqual( | ||
| stat.isDirectory, | ||
| false, | ||
| "Stat should show isDirectory false", | ||
| ); | ||
|
|
||
| // 6. Delete file | ||
| await fileFs.delete(); | ||
| const existsAfterDelete = Boolean(await fileFs.exists()); | ||
| test.assertEqual( | ||
| existsAfterDelete, | ||
| false, | ||
| "File should not exist after deletion", | ||
| ); | ||
| } catch (error) { | ||
| try { | ||
| const fileFs = fsOperation(fileUrl); | ||
| if (await fileFs.exists()) { | ||
| await fileFs.delete(); | ||
| } | ||
| } catch (_) {} | ||
| throw error; | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| runner.test("FTP: createDirectory, lsDir, delete directory", async (test) => { | ||
| if (!testDir) throw new SkipTest("No FTP_TEST_URL"); | ||
|
|
||
| const fs = fsOperation(testDir); | ||
| const dirname = `__ftp_dir_test_${Date.now()}__`; | ||
| const dirUrl = Url.join(testDir, dirname); | ||
|
|
||
| try { | ||
| // 1. Create directory | ||
| const createdDirUrl = await fs.createDirectory(dirname); | ||
| test.assertEqual( | ||
| Url.pathname(createdDirUrl), | ||
| Url.pathname(dirUrl), | ||
| "Created directory URL should match", | ||
| ); | ||
|
|
||
| const dirFs = fsOperation(createdDirUrl); | ||
| // Native FTP exists() resolves 1/0, not a boolean | ||
| const exists = Boolean(await dirFs.exists()); | ||
| test.assertEqual(exists, true, "Created directory should exist"); | ||
|
|
||
| // 2. Stat check | ||
| const stat = await dirFs.stat(); | ||
| test.assertEqual( | ||
| stat.isDirectory, | ||
| true, | ||
| "Stat should show isDirectory true", | ||
| ); | ||
| test.assertEqual(stat.isFile, false, "Stat should show isFile false"); | ||
|
|
||
| // 3. Create a file inside directory | ||
| const fileUrl = await dirFs.createFile("child.txt", "child content"); | ||
|
|
||
| // 4. List directory contents | ||
| const list = await dirFs.lsDir(); | ||
| const child = list.find((item) => item.name === "child.txt"); | ||
| test.assert( | ||
| child !== undefined, | ||
| "lsDir should list the created child file", | ||
| ); | ||
| test.assertEqual(child.isFile, true, "child item should be a file"); | ||
|
|
||
| // 5. Delete child file and directory | ||
| const childFs = fsOperation(fileUrl); | ||
| await childFs.delete(); | ||
| await dirFs.delete(); | ||
|
|
||
| const dirExistsAfterDelete = Boolean(await dirFs.exists()); | ||
| test.assertEqual( | ||
| dirExistsAfterDelete, | ||
| false, | ||
| "Directory should not exist after deletion", | ||
| ); | ||
| } catch (error) { | ||
| try { | ||
| const dirFs = fsOperation(dirUrl); | ||
| if (await dirFs.exists()) { | ||
| await dirFs.delete(); | ||
| } | ||
| } catch (_) {} | ||
| throw error; | ||
| } | ||
| }); | ||
|
|
||
| return await runner.run(writeOutput); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.