aboutsummaryrefslogtreecommitdiff
path: root/lib/memfs/memfs_test.go
AgeCommit message (Collapse)Author
3 dayslib/memfs: fix due to changes on [text/diff] packageShulhan
10 daysall: apply go fixShulhan
2026-01-15all: convert license and copyright to use SPDX identifiersShulhan
With help of spdxconv tool [1], we able to bulk update all files license and copyright format to comply with SPDX formats. [1] https://kilabit.info/project/spdxconv/
2024-12-03lib/memfs: move compiled regex to their optionsShulhan
This changes move the following fields and methods from MemFS, * incRE and excRE fields to Options, * isIncluded and isExclude methods to Options, * watchRE field to WatchOptions, * isWatched method to WatchOptions. The reason is to allow other type that use Options or WatchOptions to use isIncluded, isExclude, or isWatched methods.
2024-09-04all: fix various linter warningsShulhan
While at it, temporary disable gosec due to excessive report for G115, which may be true, but may also break the current working program. We should alter and fix once we can test and make sure that it does not breaks.
2024-07-22lib/memfs: sanitize the Root directory to fix refreshShulhan
In [MemFS.refresh], if the requested url is "/file1" and [Options.Root] is ".", the path during refresh become "file1" and if passed to [filepath.Dir] it will return ".". This cause the loop on refresh never end because there is no PathNodes equal with ".".
2024-03-21lib/memfs: trim trailing slash ("/") in the path of Get methodShulhan
The MemFS always store directory without slash. If caller request a directory node with slash, it will always return nil.
2024-03-06all: conform with linter gosec, ineffasign, and makezeroShulhan
Some of warnings from those linter are false positives, so we just annotated them.
2024-03-05lib/memfs: remove deprecated Merge functionShulhan
The Merge function has been replaced with [memfs.MemFS.Merge] for better API.
2024-03-05all: comply with linter recommendations #3Shulhan
For HTTP server that use TLS, set the minimum TLS version and ReadHeaderTimeout to mitigate slowloris attack. For HTTP client or server that parameterize the use of InsecureSkipVerify, annotate the line with "nolint:gosec" to allow the code pass the check. Library that still use sha1, in example in DKIM and TOTP, skip the warnings by annotating the line with "nolint:gosec". A pointer variable now allocated their address before assigning its value. Any error that returned now wrapped using "%w". Also, replace error checking using [errors.Is] or [errors.As] instead of using equal or not-equal operators. In "lib/http", replace any usage of "math/rand" with "crypto/rand". Any call of [math/big.Rat.SetString] now annotated with "nolint:gosec" since its false positive, the issue has been fixed in Go >= 1.17.7. Any switch case that does not cover the rest of the possible values now handled by adding the cases or by replacing the "default" case with the rest of values.
2024-03-05all: comply with linter recommendations #2Shulhan
HTTP request now implicitly create request with context. Any false positive related to not closing HTTP response body has been annotated with "nolint:bodyclose". In the example code, use consistent "// Output:" comment format, by prefixing with single space. Any comment on code now also prefixing with single space. An error returned without variables now use [errors.New] instead of [fmt.Errorf]. Any error returned using [fmt.Errorf] now wrapped using "%w" instead of "%s". Also, replace error checking using [errors.Is] or [errors.As], instead of using equal/not-equal operator. Any statement like "x = x OP y" now replaced with "x OP= y". Also, swap statement is simplified using "x, y = y, x". Any switch statement with single case now replaced with if-condition. Any call to defer on function or program that call [os.Exit], now replaced by calling the deferred function directly. Any if-else condition now replaced with switch statement, if possible.
2024-03-02all: move the repository to "git.sr.ht/~shulhan/pakakeh.go"Shulhan
There are several reasons that why we move from github.com. First, related to the name of package. We accidentally name the package with "share" a common word in English that does not reflect the content of repository. By moving to other repository, we can rename it to better and unique name, in this "pakakeh.go". Pakakeh is Minang word for tools, and ".go" suffix indicate that the repository related to Go programming language. Second, supporting open source. The new repository is hosted under sourcehut.org, the founder is known to support open source, and all their services are licensed under AGPL, unlike GitHub that are closed sources. Third, regarding GitHub CoPilot. The GitHub Terms of Service [1], allow any public content that are hosted there granted them to parse the content. On one side, GitHub helps and flourish the open source, but on another side have an issues regarding scraping the copyleft license [2]. [1]: https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#4-license-grant-to-us [2]: https://githubcopilotinvestigation.com
2024-02-03lib/memfs: fix flaky test on [Memfs.Get] with refreshShulhan
The test is flaky because we use range over map of [tdata.Input]. Since range over map will return random key, there is possibility that the first test is writing "/dir-a/dir-b/file2" not "/dir-a/dir-b/file".
2023-12-13all: fix linter warnings reported by reviveShulhan
There are some reports that I disagree with revive, in example, code should not declare the type after variables. In my opinion, on some cases, declaring the type make the code more readable and explicit. Since I did not want to add new configuration file, we changes it and follow revive for now.
2023-12-09lib/memfs: add parameter withContent to [Node.JSON]Shulhan
If the parameter withContent is true, which is default in MarshalJSON, then all of the node content will be included in JSON output. This also changes the parameter withoutModTime to withModTime because working with negation make code not easy to read.
2023-11-15lib/memfs: check for empty path on GetShulhan
2023-11-14lib/memfs: improve the refresh methodShulhan
Instead of refreshing from the Root, find the directory that closes to the requested path. While at it, simplify the returned error.
2023-11-08lib/memfs: remove mocking os.StatShulhan
The original idea of mocking the os.Stat to allow running test without adding Sleep, but this cause our tests a little bit flaky.
2023-11-05lib/memfs: include empty directoryShulhan
Even thought empty directory does not contains file, from the parent node _it is_ part of their content. Also, there is a use case where memfs use as virtual file system (VFS), as a layer with file system, where user can view list of directory, create a directory or file on the fly. If we skip scanning empty directory, that directory will not be visible.
2023-10-29lib/memfs: add method Child to NodeShulhan
The Child method return the child node based on its node.
2023-05-18lib/memfs: mock the os.Stat to fix flaky testsShulhan
In the history of our tests, we got the following test that sometime failed, 2023/05/02 14:27:32 DirWatcher: on file changes /subdir/testfile: file does not exist 2023/05/02 14:27:34 DirWatcher: on file changes /sub/index.html: file does not exist panic: test timed out after 1m0s ... goroutine 1 [chan receive]: github.com/shuLhan/share/lib/memfs.ExampleDirWatcher() share/lib/memfs/dirwatcher_example_test.go:114 +0x165f One of possible cause is that in the DirWatcher or Watcher we have the following condition // Watcher.start if w.node.ModTime().Equal(newInfo.ModTime()) { continue } // DirWatcher.processSubdirs if node.ModTime().Equal(newDirInfo.ModTime()) { continue that result to true if the node modified in the same second as it was created, which cause changes not propagated to qchanges. The fix is by mocking the os.Stat by returning the FileInfo with ModTime always increased by 1 second on every call.
2022-08-31lib/memfs: remove unused parameter in isWatched and isIncludedShulhan
While at it change the isIncluded parameter from os.FileMode to os.FileInfo.
2022-07-16lib/memfs: set the Root SysPath to the first MemFS instance on MergeShulhan
Previously, calling Merge(...), set the merged MemFS Root.SysPath to "..". Since we allow the TryDirect to access the file directly (if its set to true), this may cause the file system leaks if returned MemFS set this flag to true. To prevent that, we set the SysPath to the first MemFS SysPath.
2022-04-06lib/memfs: change the Watch method to accept structShulhan
Previously, we assume that the list of files being Watch-ed is equal to list of files being in Includes. This may not be correct. For example, we may want to watch "*.ts" files only but did not want it to be included during GoEmbed. This changes introduce list of pattern for files to be watched in the WatchOptions.Watches field. If this field is empty, only files pass the Includes filter will be watched.
2021-12-26lib/memfs: add method InitShulhan
The Init provided to initialize MemFS instance if its Options is set directly, not through New() function.
2021-12-19lib/memfs: realign struct Node, Options, PathNode, and on unit testsShulhan
The realign save storage spaces on struct, * Node: from 240 to 224 bytes (-16 bytes) * Options: from 112 to 104 bytes (-8 bytes) * PathNode: from 16 to 8 bytes (-8 bytes)
2021-10-09lib/memfs: remove PathNode "f" fieldShulhan
Previously, the PathNode has two fields to store the node in memory, one is "v" that store map of path to *Node and another is "f" that store the map of path to function that return a *Node. The "f" is used to handle embedding Go generated code, but since the template now can handle generated dynamic path and Node this field is not used anymore.
2021-10-09lib/memfs: prefix all error when the method or function namesShulhan
One of the hardest when multiple packages is used is to detect where there error happened. For example, if a program return an error io.EOF, it's hard to detect the exact method or function that caused its, especially when processing multiple files with network connection.
2021-10-09lib/memfs: refactoring Node field V into ContentShulhan
The reason why the field named V is because it's short. It's come from my C/C++ experience that got carried away when writing this package. Now, after having more time writing Go, I prefer clarity over cleverity(?).
2021-10-09lib/memfs: simplify createRoot and scanDirShulhan
The createRoot should not open the file, only call to os.Stat. The scanDir should only accept the node to be scanned and the call to Open and Close should be inside the method of scanDir not outside of it. While at it, prefix all returned errors with method names.
2021-08-29lib/memfs: change the MarshalJSON to always return an objectShulhan
Previously, MarshalJSON on memfs will return an object of map of all PathNodes and on Node it will return an object. This changes make it the JSON response consistent. If its directory it will return the node object with its childs, without "content". If its file, it will return the node object with content. While at it, use single "mod_time" with value is epoch and return the node ContentType as "content_type".
2021-08-22lib/memfs: add method Save and EncodeShulhan
The Save method will write the new content to file system and update the content of Node using Encode().
2021-08-22lib/memfs: fix empty file not being added to treeShulhan
Previously, we did not check if the file size is 0 before reading the content or updating the content type, which cause the read on file return io.EOF and the file not added to caches. This commit fix this issue by checking for zero file size and for io.EOF when reading the file content.
2021-08-22lib/memfs: fix symbolic link with different nameShulhan
Previously, if file is symbolic link and has different name with their original file, it will return an error when we tried to open the file parentpath/filename: no such file or directory because we use the original file name, not the symlinked file name. This commit fix this issue by not replacing the original FileInfo for symlink but by setting only the size and mode.
2021-07-30lib/memfs: fix the unit test due to different modTime result after cloneShulhan
If someone clone this repository, all the files modTime in the directory testdata will be set to the current time. This will cause the test fail. This changes fix this issue by setting the modTime of all Node to static, fixed time value.
2021-07-25lib/memfs: implement json.Marshaler on MemFS and NodeShulhan
Previously, without MarshalJSON, encoding the MemFS or Node object will result in incomplete information, for example, missing name, modification time, and size. This commit implement the json.Marshaler in MemFS which encode the PathNode sorted by keys in ascending order.
2021-07-18lib/memfs: reorder the test functions by namesShulhan
While at it, rename the test functions by the function or method that they tests.
2021-07-08lib/memfs: fix unit test due changes on Merge functionShulhan
2021-06-03memfs: add function to Merge one or more instance of MemFSShulhan
The Merge function merge one or more instance of MemFS into single MemFS instance. If there are two instance of Node that have the same path, the last instance will be ignored.
2021-05-04memfs: fix test by checking multiple content-typesShulhan
On Arch Linux with Go tip, the content-type for JavaScript file is "text/javascript". While on Ubuntu with Go 1.16 the content-type for JavaScript file is "application/javascript".
2021-04-27memfs: update the test ContentType for JavaScript filesShulhan
The latest Go set the content type for JavaScript file as "text/javascript; charset=utf-8", not "application/javascript".
2021-03-14all: refactoring the test.Assert and test.AssertBench signatureShulhan
Previously, the test.Assert and test.AssertBench functions has the boolean parameter to print the stack trace of test in case its not equal. Since this parameter is not mandatory and its usually always set to "true", we remove them from function signature to simplify the call to Assert and AssertBench.
2021-01-09memfs: allow AddFile to set internal pathShulhan
Previously, AddFile set the internal path equal to path of file to be included. This may cause conflict if the file is already included due to the same sys path but different internal path. This commit add parameter internalPath to set custom internal path in the memfs map.
2021-01-08memfs: remove field WithContentShulhan
The field WithContent is not necessary if we set MaxFileSize to negative value.
2021-01-08memfs: refactoring, allow multiple instances of memfs in single packageShulhan
Previously, the generated Go code from memfs can be used only once on the package that use it. For example, if we have two instances of memfs.MemFS and both of them call GoGenerate(), when we load them back again only the last one will be active and set the global variable memfs.GeneratedPathNode. This changes refactoring on how we use memfs by storing the generated path node into variable that is defined by user and pass them to New Options. This changes affect package http and io.
2020-06-06all: use default linter optionsShulhan
2020-05-07memfs: make the Node Readdir works when calling from generated Go codeShulhan
Previously, the Go generated code from call to GoGenerate() set only mapping of path to node. Calling Readdir() using http.File using eoot, err := mfs.Open("/") fis, err := root.Readdir(0) will return nil on fis. This changes add all child nodes to generated node.
2020-05-07memfs: simplify the API, add directory parameter to NewShulhan
This changes add parameter directory to be mounted in New(), so user did not need to call Mount manually This cause the Mount method and its sibling, Unmount and IsMounted become unneeded, so we remove them.
2020-04-23memfs: fix test based on changes on 8b030bebShulhan
2019-12-09memfs: implement os.FileInfo on NodeShulhan