From 77c94da3a144fa6c90e9f1684c94aa1ee94620d7 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 09:16:46 +0300 Subject: [PATCH 01/33] feat(#3251): added file, dir and tmpdir to eo-runtime --- eo-runtime/src/main/eo/org/eolang/fs/dir.eo | 112 ++++++ eo-runtime/src/main/eo/org/eolang/fs/file.eo | 291 ++++++++++++++ .../src/main/eo/org/eolang/fs/tmpdir.eo | 76 ++++ .../src/main/eo/org/eolang/io/console.eo | 13 +- .../main/eo/org/eolang/io/malloc-as-output.eo | 2 +- .../src/main/eo/org/eolang/sys/getenv.eo | 34 ++ .../main/eo/org/eolang/sys/line-separator.eo | 12 +- .../eo/org/eolang/sys/{uname.eo => os.eo} | 15 +- .../EOeolang/EOfs/EOdir$EOmade$EOmkdir.java | 57 +++ .../EOfs/EOdir$EOtmpfile$EOtouch.java | 60 +++ .../EOorg/EOeolang/EOfs/EOdir$EOwalk.java | 85 ++++ .../EOfs/EOfile$EOdeleted$EOdelete.java | 59 +++ .../EOorg/EOeolang/EOfs/EOfile$EOexists.java | 60 +++ .../EOeolang/EOfs/EOfile$EOis_directory.java | 57 +++ .../EOeolang/EOfs/EOfile$EOmoved$EOmove.java | 61 +++ ...pen$EOfile_stream$EOread$EOread_bytes.java | 73 ++++ ...EOfile_stream$EOwrite$EOwritten_bytes.java | 72 ++++ .../EOfs/EOfile$EOopen$EOprocess_file.java | 63 +++ .../EOfs/EOfile$EOresolved$EOresolve.java | 56 +++ .../EOorg/EOeolang/EOfs/EOfile$EOsize.java | 55 +++ .../EOfs/EOfile$EOtouched$EOtouch.java | 55 +++ .../main/java/EOorg/EOeolang/EOfs/Files.java | 139 +++++++ .../EOorg/EOeolang/EOfs/package-info.java | 31 ++ .../java/EOorg/EOeolang/EOsys/EOgetenv.java | 65 +++ .../EOorg/EOeolang/EOsys/EOos$EOname.java | 10 +- .../src/test/eo/org/eolang/fs/dir-test.eo | 102 +++++ .../src/test/eo/org/eolang/fs/file-tests.eo | 370 ++++++++++++++++++ .../src/test/eo/org/eolang/fs/tmpdir-tests.eo | 40 ++ .../eolang/sys/{uname-test.eo => os-test.eo} | 8 +- .../java/EOorg/EOeolang/EOfs/FilesTest.java | 4 + 30 files changed, 2109 insertions(+), 28 deletions(-) create mode 100644 eo-runtime/src/main/eo/org/eolang/fs/dir.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/fs/file.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/fs/tmpdir.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/sys/getenv.eo rename eo-runtime/src/main/eo/org/eolang/sys/{uname.eo => os.eo} (81%) create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOmade$EOmkdir.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOtmpfile$EOtouch.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOdeleted$EOdelete.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOprocess_file.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOsize.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOfs/package-info.java create mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java rename "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOuname$EO\317\206.java" => eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java (90%) create mode 100644 eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo create mode 100644 eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo create mode 100644 eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo rename eo-runtime/src/test/eo/org/eolang/sys/{uname-test.eo => os-test.eo} (93%) create mode 100644 eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java diff --git a/eo-runtime/src/main/eo/org/eolang/fs/dir.eo b/eo-runtime/src/main/eo/org/eolang/fs/dir.eo new file mode 100644 index 0000000000..453e84f34d --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/fs/dir.eo @@ -0,0 +1,112 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.txt.sprintf ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++package org.eolang.fs ++rt jvm org.eolang:eo-runtime:0.0.0 ++rt node eo2js-runtime:0.0.0 ++version 0.0.0 + +# Directory in the file system. +# Apparently every directory is a file. +[file] > dir + $.file > @ + true > is-directory + + # Makes a directory together with all required + # parent directories and returns the created directory. + [] > made + if. > @ + ^.exists + ^ + seq + * + mkdir + ^ + + # Makes a directory together with all required + # parent directories. + # + # Attention! The object is for internal usage only, please + # don't use it programmatically outside of `dir` object. + [] > mkdir /true + + # Goes though all files in the directory, recursively + # finding them with the `glob` provided. + # Returns `tuple` of all files in the directory. + [glob] > walk /tuple + + # Deletes directory and all files in it, recursively. + # Returns the deleted directory. + [] > deleted + (^.walk "**").at.^ > walked + walked.length > len! + if. > @ + ^.exists + seq + * + rec-delete walked 0 + ^ + ^ + + # Deletes files and directories in current directory recursively. + # Returns `true`. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `dir` object. + [tup index] > rec-delete + if. > @ + ^.len.eq index + true + seq + * + tup.tail.deleted.exists + ^.rec-delete + tup.head + index.plus 1 + + # Creates an empty temporary file in the current directory. + [] > tmpfile + if. > @ + ^.exists + QQ.fs.file touched.as-bytes.as-string + error + sprintf + "Directory %s does not exist, can't create temporary file" + * ^.path + + # Creates an empty temporary file in the current directory and + # returns absolute path to it as `string`. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of the `dir` object. + [] > touch /string + + # Opens the file for I/O operations. + # Since current file is a directory - returns an `error`. + [mode scope] > open + error > @ + sprintf + "The file %s is a directory, can't open for I/O operations" + * ^.path diff --git a/eo-runtime/src/main/eo/org/eolang/fs/file.eo b/eo-runtime/src/main/eo/org/eolang/fs/file.eo new file mode 100644 index 0000000000..7008b56d83 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/fs/file.eo @@ -0,0 +1,291 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.sys.os ++alias org.eolang.txt.sprintf ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++package org.eolang.fs ++rt jvm org.eolang:eo-runtime:0.0.0 ++rt node eo2js-runtime:0.0.0 ++version 0.0.0 + +# A file on the filesystem. +[path] > file + path > @ + + # Returns `true` if current file is a directory, returns `false` otherwise. + [] > is-directory /bool + + # The system-dependent default name-separator character. + # On UNIX systems the value of this field is "/"; + # on Microsoft Windows systems it is "\\". + [] > separator + string > @ + as-bytes. + if. + os.is-windows + "\\" + "/" + + # Resolves `other` path against `path` and returns as new `file` from it. + [other] > resolved + QQ.fs.file resolve.as-bytes.as-string > @ + + # Resolves `other` path against `^.path` and returns as `string`. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [] > resolve /string + + # Returns `true` if file with current `path` exists in filesystem. + [] > exists /bool + + # If current file exists - returns the file. + # If current file does not exist - create an empty file + # in filesystem and returns it. + [] > touched + if. > @ + ^.exists + ^ + seq + * + touch + ^ + + # Creates new empty file. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [] > touch /true + + # If current file exists - deletes it and returns it. + # If current file does not exist - just returns it. + [] > deleted + if. > @ + ^.exists + seq + * + delete + ^ + ^ + + # Deletes the file and and returns `true`. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [] > delete /true + + # Get file size in bytes. + [] > size /number + + # Move current file to `target`, making and returning a new `file` from it. + [target] > moved + QQ.fs.file move.as-bytes.as-string > @ + + # Tries to move file from `^.path` to `target` + # and returns path of moved file as `string`. + # Returns `error` is failed to move the file. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [] > move /string + + # Opens the file. + # + # The first argument `mode` defines the operations that are allowed on the file + # and how these operations are performed. + # + # The access mode can be: + # |------|-------------|----------------------|-------------------|----------------------| + # | Mode | Description | File Existence | Init pointer pos | Content Handling | + # |======|=============|======================|===================|======================| + # | "r" | Read only | Must exist | Beginning | No changes | + # |------|-------------|----------------------|-------------------|----------------------| + # | "w" | Write only | Created if not exist | Beginning | Truncated if exists, | + # | | | | | created if not | + # |------|-------------|----------------------|-------------------|----------------------| + # | "a" | Append only | Created if not exist | End | No truncation, | + # | | | | | created if not | + # |------|-------------|----------------------|-------------------|----------------------| + # | "r+" | Read/Write | Must exist | Beginning | No changes | + # |------|-------------|----------------------|-------------------|----------------------| + # | "w+" | Write/Read | Created if not exist | Beginning | Truncated if exists, | + # | | | | | created if not | + # |------|-------------|----------------------|-------------------|----------------------| + # | "a+" | Read/Append | Created if not exist | End (for writing) | No truncation, | + # | | | | | created if not | + # |------|-------------|----------------------|-------------------|----------------------| + # + # The second argument `scope` is the scope where file is available as input + # (the `read` object is available) or output (the `write` object is available). + # + # When `file.open` is dataized - it opens file stream, dataizes the `scope`, + # closes the file stream and returns an original file object. + [mode scope] > open + mode > access! + access.eq "r" > read + access.eq "w" > write + access.eq "a" > append + access.eq "r+" > read-write + access.eq "w+" > write-read + access.eq "a+" > read-append + as-bool. > can-read + or. + read.or read-write + write-read.or read-append + as-bool. > can-write + or. + or. + write.or read-write + write-read.or read-append + append + as-bool. > must-exists + read.or read-write + as-bool. > truncate + write.or write-read + if. > @ + can-read.not.and can-write.not + error "Wrong access mod. Only next modes are available: 'r', 'w', 'a', 'r+', 'w+', 'a+'" + if. + ^.exists.not + if. + must-exists + error + sprintf + "File must exist for given access mod: '%s'" + * access + seq + * + ^.touched.touch + process-file + ^ + if. + truncate + seq + * + ^.deleted.delete + ^.touched.touch + process-file + ^ + seq + * + process-file + ^ + + # Process current file in the provided scope. + # + # Here file stream is open, then the stream is passed to `scope`, + # then given `scope` is dataized and stream is closed. + # Returns `true` if there are no errors occurred while `scope` + # dataization, returns `error` otherwise. + # + # The object is stream-safe, which means that stream is closed anyway, + # even if errors are occurred while `scope` dataization. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of the `file` object. + [] > process-file /true + + # File stream. + # The objects provides an API for using file as input or output. + [] > file-stream + # Read `size` amount of bytes from file input stream. + # Returns new instance of `input-block` with `buffer` read from file, or + # returns `error` if access mode does not allow reading operations. + [size] > read + ((input-block --).read size).self > @ + + # File input block + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [buffer] > input-block + $ > self + buffer > @ + + # Read `size` amount of bytes from file input stream. + # Returns new instance of `input-block` with `buffer` read from file, or + # returns `error` if provided access mode does not allow reading operations. + [size] > read + ^.^.read-bytes size > read-bytes! + self. > @ + if. + ^.^.^.^.can-read.not + [] >> + $ > self + error > @ + sprintf + "Can't read from file with provided access mode '%s'" + * ^.^.^.^.^.access + seq + * + read-bytes + ^.^.input-block read-bytes + + # Bytes read from file input stream + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [size] > read-bytes /bytes + + # Write given `buffer` to file output stream. + # Here `buffer` is either sequence of bytes or and object that can be + # dataized via `as-bytes` object. + # Returns new instance of `output-block` ready to write again, or + # returns an error if provided access mode does not allow writing operations. + [buffer] > write + (output-block.write buffer).self > @ + + # File output block. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [] > output-block + $ > self + true > @ + + # Write given `buffer` to file output stream. + # Here `buffer` is either sequence of bytes or and object that can be + # dataized via `as-bytes` object. + # Returns new instance of `output-block` ready to write again, or + # returns an error if provided access mode does not allow writing operations. + [buffer] > write + self. > @ + if. + ^.^.^.^.can-write.not + [] >> + $ > self + error > @ + sprintf + "Can't write to file with provided access mode '%s'" + * ^.^.^.^.^.access + seq + * + ^.^.written-bytes buffer + ^.^.output-block + + # Bytes written to file output stream. + # + # Attention! The object is for internal usage only, please + # don't use the object programmatically outside of `file` object. + [buffer] > written-bytes /true diff --git a/eo-runtime/src/main/eo/org/eolang/fs/tmpdir.eo b/eo-runtime/src/main/eo/org/eolang/fs/tmpdir.eo new file mode 100644 index 0000000000..eed2a58025 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/fs/tmpdir.eo @@ -0,0 +1,76 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.fs.dir ++alias org.eolang.fs.file ++alias org.eolang.sys.getenv ++alias org.eolang.sys.os ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++package org.eolang.fs ++rt jvm org.eolang:eo-runtime:0.0.0 ++rt node eo2js-runtime:0.0.0 ++version 0.0.0 + +# Temporary directory. +# For Unix/MacOS uses the path supplied by the first environment variable +# found in the list TMPDIR, TMP, TEMP, TEMPDIR. +# If none of these are found, "/tmp" is used. +# +# For Windows uses the path reported by the Windows "GetTempPath" API function which +# takes the first environment variable in the list TMP, TEMP, USERPROFILE. +# If none of these are found, it returns the Windows directory (C:\Windows). +[] > tmpdir + dir > @ + file + string + [] > os-tmp-dir! + getenv "TMPDIR" > tmpdir! + getenv "TMP" > tmp! + getenv "TEMP" > temp! + getenv "TEMPDIR" > tempdir! + getenv "USERPROFILE" > userprofile! + if. > @ + os.is-windows + if. + tmp.eq "" + if. + temp.eq "" + if. + userprofile.eq "" + "C:\\Windows" + userprofile + temp + tmp + if. + tmpdir.eq "" + if. + tmp.eq "" + if. + temp.eq "" + if. + tempdir.eq "" + "/tmp" + tempdir + temp + tmp + tmpdir diff --git a/eo-runtime/src/main/eo/org/eolang/io/console.eo b/eo-runtime/src/main/eo/org/eolang/io/console.eo index 2904576727..8a8250df4d 100644 --- a/eo-runtime/src/main/eo/org/eolang/io/console.eo +++ b/eo-runtime/src/main/eo/org/eolang/io/console.eo @@ -96,9 +96,9 @@ # Write given `buffer` to console. # Here `buffer` is either sequence of bytes or and object that can be # dataized via `as-bytes` object. + # Returns new instance of `output-block` ready to write again. [buffer] > write - self. > @ - output-block.write buffer + (output-block.write buffer).self > @ # Console output block. # @@ -111,10 +111,11 @@ # Writes bytes contained in `buffer` to operation system console. # Returns new instance of `output-block` ready to write again. [buffer] > write - seq > @ - * - ^.^.written-bytes buffer - ^.^.output-block + self. > @ + seq + * + ^.^.written-bytes buffer + ^.^.output-block # Bytes written to operation system console. # diff --git a/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo b/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo index 21fce98cd1..bc529d2ac5 100644 --- a/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo +++ b/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo @@ -71,4 +71,4 @@ * ^.^.^.allocated.write offset buffer ^.^.output-block - offset.plus buffer.size + offset.plus buffer.size \ No newline at end of file diff --git a/eo-runtime/src/main/eo/org/eolang/sys/getenv.eo b/eo-runtime/src/main/eo/org/eolang/sys/getenv.eo new file mode 100644 index 0000000000..802612c547 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/sys/getenv.eo @@ -0,0 +1,34 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++package org.eolang.sys ++rt jvm org.eolang:eo-runtime:0.0.0 ++rt node eo2js-runtime:0.0.0 ++version 0.0.0 + +# Get environment variable as `string`. +# If return `string` is empty - the variable does not exist. +# +# See https://man7.org/linux/man-pages/man3/getenv.3.html +[name] > getenv /string diff --git a/eo-runtime/src/main/eo/org/eolang/sys/line-separator.eo b/eo-runtime/src/main/eo/org/eolang/sys/line-separator.eo index a31554b494..d54b1b1107 100644 --- a/eo-runtime/src/main/eo/org/eolang/sys/line-separator.eo +++ b/eo-runtime/src/main/eo/org/eolang/sys/line-separator.eo @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -+alias org.eolang.sys.uname ++alias org.eolang.sys.os +architect yegor256@gmail.com +home https://github.com/objectionary/eo +package org.eolang.sys @@ -32,7 +32,9 @@ # On UNIX systems, it returns "\n"; # on Microsoft Windows systems it returns "\r\n". [] > line-separator - if. > @ - uname.is-windows - "\r\n" - "\n" + string > @ + as-bytes. + if. + os.is-windows + "\r\n" + "\n" diff --git a/eo-runtime/src/main/eo/org/eolang/sys/uname.eo b/eo-runtime/src/main/eo/org/eolang/sys/os.eo similarity index 81% rename from eo-runtime/src/main/eo/org/eolang/sys/uname.eo rename to eo-runtime/src/main/eo/org/eolang/sys/os.eo index 8fd317a9ee..553805b87f 100644 --- a/eo-runtime/src/main/eo/org/eolang/sys/uname.eo +++ b/eo-runtime/src/main/eo/org/eolang/sys/os.eo @@ -28,10 +28,11 @@ +rt node eo2js-runtime:0.0.0 +version 0.0.0 -# Represents the name of the operation system. -# See https://man7.org/linux/man-pages/man2/uname.2.html. -[] > uname - [] > @ /string - ((regex "/^Windows.+$/").matches @).as-bool > is-windows - ((regex "/linux/i").matches @).as-bool > is-linux - ((regex "/mac/i").matches @).as-bool > is-macos +# Represents the name of the operating system. +[] > os + name > @ + ((regex "/^Windows.+$/").matches name).as-bool > is-windows + ((regex "/linux/i").matches name).as-bool > is-linux + ((regex "/mac/i").matches name).as-bool > is-macos + + [] > name /string diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOmade$EOmkdir.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOmade$EOmkdir.java new file mode 100644 index 0000000000..f7d6a0a4f7 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOmade$EOmkdir.java @@ -0,0 +1,57 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * Dir.made.mkdir. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "dir.made.mkdir") +public final class EOdir$EOmade$EOmkdir extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + return new Data.ToPhi( + Paths.get( + new Dataized( + this.take(Attr.RHO).take(Attr.RHO).take("file").take("path") + ).asString() + ).toFile().mkdirs() + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOtmpfile$EOtouch.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOtmpfile$EOtouch.java new file mode 100644 index 0000000000..67c5c82f79 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOtmpfile$EOtouch.java @@ -0,0 +1,60 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * Dir.tmpfile.touch. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "dir.tmpfile.touch") +public final class EOdir$EOtmpfile$EOtouch extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + final Path home = Paths.get( + new Dataized( + this.take(Attr.RHO).take(Attr.RHO).take("path") + ).asString() + ); + final Path path = Files.createTempFile(home, null, null); + path.toFile().deleteOnExit(); + return new Data.ToPhi(path.toAbsolutePath().toString()); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java new file mode 100644 index 0000000000..85b9497154 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java @@ -0,0 +1,85 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.Paths; +import org.eolang.AtVoid; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * Dir.walk. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "dir.walk") +public final class EOdir$EOwalk extends PhDefault implements Atom { + /** + * Ctor. + */ + public EOdir$EOwalk() { + this.add("glob", new AtVoid("glob")); + } + + @Override + public Phi lambda() throws Exception { + final Path path = Paths.get( + new Dataized( + this.take(Attr.RHO).take("file").take("path") + ).asString() + ).toAbsolutePath(); + final String glob = new Dataized( + this.take("glob") + ).asString(); + final PathMatcher matcher = FileSystems.getDefault().getPathMatcher( + String.format("glob:%s", glob) + ); + return new Data.ToPhi( + Files.walk(path) + .map(p -> p.toAbsolutePath().toString()) + .map(p -> p.substring(p.indexOf(path.toString()))) + .filter(p -> matcher.matches(Paths.get(p))) + .map(p -> { + final Phi file = Phi.Φ.take("org.eolang.fs.file").copy(); + file.put(0, new ToPhi(p)); + return file; + }) + .toArray(Phi[]::new) + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOdeleted$EOdelete.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOdeleted$EOdelete.java new file mode 100644 index 0000000000..b599af1b96 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOdeleted$EOdelete.java @@ -0,0 +1,59 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Files; +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.deleted.delete. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.deleted.delete") +public final class EOfile$EOdeleted$EOdelete extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + Files.delete( + Paths.get( + new Dataized( + this.take(Attr.RHO).take(Attr.RHO).take("path") + ).asString() + ) + ); + return new Data.ToPhi(true); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java new file mode 100644 index 0000000000..7ae74a7ff0 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java @@ -0,0 +1,60 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Files; +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.exists. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.exists") +public final class EOfile$EOexists extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + return new Data.ToPhi( + Files.exists( + Paths.get( + new Dataized( + this.take(Attr.RHO).take("path") + ).asString() + ) + ) + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java new file mode 100644 index 0000000000..496c421e4f --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java @@ -0,0 +1,57 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.is-directory. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.is-directory") +public final class EOfile$EOis_directory extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + return new Data.ToPhi( + Paths.get( + new Dataized( + this.take(Attr.RHO).take("path") + ).asString() + ).toFile().isDirectory() + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java new file mode 100644 index 0000000000..7830c7de2b --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java @@ -0,0 +1,61 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.moved.move. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.moved.move") +public final class EOfile$EOmoved$EOmove extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + final Phi rho = this.take(Attr.RHO); + final Path target = Paths.get( + new Dataized(rho.take("target")).asString() + ); + Files.move( + Paths.get(new Dataized(rho.take(Attr.RHO).take("path")).asString()), + target + ); + return new Data.ToPhi(target.toString()); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java new file mode 100644 index 0000000000..2fcf7c2ea4 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java @@ -0,0 +1,73 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.AtVoid; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.open.file-stream.read.read-bytes. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.open.file-stream.read.read-bytes") +public final class EOfile$EOopen$EOfile_stream$EOread$EOread_bytes extends PhDefault implements Atom { + + /** + * Ctor. + */ + public EOfile$EOopen$EOfile_stream$EOread$EOread_bytes() { + this.add("size", new AtVoid("size")); + } + + @Override + public Phi lambda() throws Exception { + return new Data.ToPhi( + Files.INSTANCE.read( + Paths.get( + new Dataized( + this.take(Attr.RHO) + .take(Attr.RHO) + .take(Attr.RHO) + .take(Attr.RHO) + .take("path") + ).asString() + ).toString(), + new Dataized(this.take("size")).asNumber().intValue() + ) + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java new file mode 100644 index 0000000000..517da88ef5 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java @@ -0,0 +1,72 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.AtVoid; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.open.file-stream.write.written-bytes. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.open.file-stream.write.written-bytes") +public final class EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes extends PhDefault implements Atom { + + /** + * Ctor. + */ + public EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes() { + this.add("buffer", new AtVoid("buffer")); + } + + @Override + public Phi lambda() throws Exception { + Files.INSTANCE.write( + Paths.get( + new Dataized( + this.take(Attr.RHO) + .take(Attr.RHO) + .take(Attr.RHO) + .take(Attr.RHO) + .take("path") + ).asString() + ).toString(), + new Dataized(this.take("buffer")).take() + ); + return new Data.ToPhi(true); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOprocess_file.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOprocess_file.java new file mode 100644 index 0000000000..ecb25cbfd7 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOprocess_file.java @@ -0,0 +1,63 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.open.process-file. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.open.process-file") +public final class EOfile$EOopen$EOprocess_file extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + final Phi open = this.take(Attr.RHO); + final String name = Paths.get( + new Dataized(open.take(Attr.RHO).take("path")).asString() + ).toString(); + Files.INSTANCE.open(name); + try { + final Phi scope = open.take("scope").copy(); + scope.put(0, open.take("file-stream")); + new Dataized(scope).take(); + } finally { + Files.INSTANCE.close(name); + } + return new Data.ToPhi(true); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java new file mode 100644 index 0000000000..3b85fa828a --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.resolved.resolve. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.resolved.resolve") +public final class EOfile$EOresolved$EOresolve extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + final Phi rho = this.take(Attr.RHO); + return new Data.ToPhi( + Paths.get(new Dataized(rho.take(Attr.RHO).take("path")).asString()) + .resolve(new Dataized(rho.take("other")).asString()) + .toString() + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOsize.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOsize.java new file mode 100644 index 0000000000..6f28e32b1e --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOsize.java @@ -0,0 +1,55 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.io.File; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.size. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.size") +public final class EOfile$EOsize extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + return new Data.ToPhi( + new File( + new Dataized(this.take(Attr.RHO).take("path")).asString() + ).length() + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java new file mode 100644 index 0000000000..46bab632be --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java @@ -0,0 +1,55 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Paths; +import org.eolang.Atom; +import org.eolang.Attr; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * File.touched.touch. + * + * @since 0.40 + * @checkstyle TypeNameCheck (100 lines) + */ +@XmirObject(oname = "file.touched.touch") +public class EOfile$EOtouched$EOtouch extends PhDefault implements Atom { + @Override + public Phi lambda() throws Exception { + return new Data.ToPhi( + Paths.get( + new Dataized(this.take(Attr.RHO).take(Attr.RHO).take("path")).asString() + ).toFile().createNewFile() + ); + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java new file mode 100644 index 0000000000..01a93b21a1 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java @@ -0,0 +1,139 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.concurrent.ConcurrentHashMap; +import org.eolang.ExFailure; +import org.eolang.Versionized; + +/** + * File streams. + * @since 0.40 + */ +@Versionized +final class Files { + /** + * Files instance + */ + static final Files INSTANCE = new Files(); + + /** + * File input streams for reading. + */ + private final ConcurrentHashMap streams = new ConcurrentHashMap<>(0); + + /** + * Ctor. + */ + private Files() { + // singleton :( + } + + /** + * Open file for reading and writing. + * @param name Name of the file + * @throws FileNotFoundException If can't open file + */ + void open(final String name) throws FileNotFoundException { + this.streams.putIfAbsent(name, new Object[] { + new FileInputStream(name), + new FileOutputStream(name, true) + }); + } + + /** + * Read given amount of bytes from file. + * @param name Name of file + * @param size Amount of bytes to read + * @return Read bytes + * @throws IOException If fails to read + */ + byte[] read(final String name, final int size) throws IOException { + synchronized (this.streams) { + if (!this.streams.containsKey(name)) { + throw new ExFailure( + "File input stream with name %s is absent, can't read", + name + ); + } + final byte[] read = new byte[size]; + int character; + int processed = 0; + final InputStream input = (InputStream) this.streams.get(name)[0]; + while (processed < size && (character = input.read()) != -1) { + read[processed] = (byte) character; + ++processed; + } + return Arrays.copyOf(read, processed); + } + } + + /** + * Write given byte buffer to file. + * @param name Name of the file + * @param buffer Byte buffer to write + * @throws IOException If fails to write + */ + void write(final String name, final byte[] buffer) throws IOException { + synchronized (this.streams) { + if (!this.streams.containsKey(name)) { + throw new ExFailure( + "File output stream with name %s is absent, can't read", + name + ); + } + ((OutputStream) this.streams.get(name)[1]).write(buffer); + } + } + + /** + * Close files streams. + * @param name File name + * @throws IOException If fails to close the streams + */ + void close(final String name) throws IOException { + synchronized (this.streams) { + if (!this.streams.containsKey(name)) { + throw new ExFailure( + "File streams with name %s is absent, can't close", + name + ); + } + ((InputStream) this.streams.get(name)[0]).close(); + ((OutputStream) this.streams.get(name)[1]).close(); + this.streams.remove(name); + } + } +} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/package-info.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/package-info.java new file mode 100644 index 0000000000..756ccd05a3 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/package-info.java @@ -0,0 +1,31 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * EO runtime, FS. + * + * @since 0.40 + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOfs; diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java new file mode 100644 index 0000000000..92446d50d5 --- /dev/null +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java @@ -0,0 +1,65 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (4 lines) + */ +package EOorg.EOeolang.EOsys; + +import org.eolang.AtVoid; +import org.eolang.Atom; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhDefault; +import org.eolang.Phi; +import org.eolang.XmirObject; + +/** + * Getenv. + * @since 0.40 + * @checkstyle TypeNameCheck (5 lines) + */ +@XmirObject(oname = "getenv") +public final class EOgetenv extends PhDefault implements Atom { + /** + * Ctor. + */ + public EOgetenv() { + this.add("name", new AtVoid("name")); + } + + @Override + public Phi lambda() throws Exception { + final String env = System.getenv( + new Dataized(this.take("name")).asString() + ); + final Phi var; + if (env != null) { + var = new Data.ToPhi(env); + } else { + var = new Data.ToPhi(""); + } + return var; + } +} diff --git "a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOuname$EO\317\206.java" b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java similarity index 90% rename from "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOuname$EO\317\206.java" rename to eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java index 5b02716500..c4c7faeb22 100644 --- "a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOuname$EO\317\206.java" +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java @@ -34,12 +34,12 @@ import org.eolang.XmirObject; /** - * Uname.@. - * @since 0.39.0 + * Os.name + * @since 0.40 * @checkstyle TypeNameCheck (5 lines) */ -@XmirObject(oname = "uname.@") -public final class EOuname$EOφ extends PhDefault implements Atom { +@XmirObject(oname = "os.name") +public final class EOos$EOname extends PhDefault implements Atom { /** * Operating system name as {@link Phi}. */ @@ -47,6 +47,6 @@ public final class EOuname$EOφ extends PhDefault implements Atom { @Override public Phi lambda() throws Exception { - return EOuname$EOφ.OS_NAME; + return EOos$EOname.OS_NAME; } } diff --git a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo new file mode 100644 index 0000000000..1762c4ce5f --- /dev/null +++ b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo @@ -0,0 +1,102 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.fs.dir ++alias org.eolang.fs.tmpdir ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++tests ++package org.eolang.fs ++version 0.0.0 + +# Test. +[] > bound-tmpfile-does-not-recreates-file + tmpdir.tmpfile > f + f.path.eq f.path > @ + +# test. +[] > makes-new-directory + made. > d + dir + tmpdir.tmpfile.deleted.resolved "foo" + and. > @ + d.exists + d.is-directory + +# Test. +[] > deletes-empty-directory + dir + tmpdir.tmpfile.deleted.resolved + "bar" + .made + .deleted + .exists + .not > @ + +# Test. +[] > throws-on-opening-directory + tmpdir.open > @ + "w" + true > [d] + +# Test. +[] > deletes-directory-with-files-recursively + (dir tmpdir.tmpfile.deleted).made > d + d.tmpfile > first + d.tmpfile > second + and. > @ + and. + and. + and. + first.exists + second.exists + d.deleted.exists.not + first.exists.not + second.exists.not + +# Test. +[] > deletes-directory-with-file-and-dir + (dir tmpdir.tmpfile.deleted).made > d + (dir d.tmpfile.deleted).made > inner + d.tmpfile > f + and. > @ + and. + and. + and. + and. + inner.is-directory + inner.exists + f.exists + d.deleted.exists.not + inner.exists.not + f.exists.not + +# Test. +[] > walks-recursively + (dir tmpdir.tmpfile.deleted).made > d + seq > @ + * + (dir (d.resolved "foo/bar")).made + (d.resolved "foo/bar/test.txt").touched + (dir (d.resolved "x/y/z")).made + (d.resolved "x/y/z/a.txt").touched + (d.walk "**/*.txt").length.eq 2 diff --git a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo new file mode 100644 index 0000000000..8217d3ab22 --- /dev/null +++ b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo @@ -0,0 +1,370 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.fs.file ++alias org.eolang.fs.tmpdir ++alias org.eolang.txt.sprintf ++alias org.eolang.txt.text ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++tests ++package org.eolang.fs ++version 0.0.0 + +# Test. +[] > check-if-current-directory-is-directory + (file ".").is-directory > @ + +# Test. +[] > check-if-absent-file-does-not-exist + (file "absent.txt").exists.not > @ + +# Test. +[] > returns-self-after-deleting + tmpdir.tmpfile > temp + temp.deleted.path.eq temp.path > @ + +# Test. +[] > returns-self-after-touching + tmpdir.tmpfile > temp + temp.deleted.touched.path.eq temp.path > @ + +# Test. +[] > checks-file-does-not-exist-after-deleting + tmpdir.tmpfile.deleted.exists.not > @ + +# Test. +[] > touches-a-file + tmpdir.tmpfile.deleted.touched.exists > @ + +# Test. +[] > measures-empty-file-after-touching + tmpdir.tmpfile.deleted.touched.size.eq 0 > @ + +# Test. +[] > does-not-fail-on-double-touching + tmpdir.tmpfile.deleted.touched.touched.exists > @ + +# Test. +[] > does-not-fail-on-double-deleting + tmpdir.tmpfile.deleted.deleted.exists.not > @ + +# Test. +[] > resolves-in-a-right-way + eq. > @ + (tmpdir.resolved "foo/bar").path + tmpdir.path.concat "foo/bar" + +# Test. +[] > throws-an-error-on-touching-temp-file-in-absent-dir + (tmpdir.resolve "foo").tmpfile > @ + +# Test. +[] > resolves-and-touches + tmpdir.resolved "foo/bar" > resolved + resolved.tmpfile > f + seq > @ + * + resolved.deleted.made.path + and. + f.exists + contains. + text f.path + "/foo/bar" + +# Test. +[] > moves-a-file + tmpdir.tmpfile > temp + sprintf > dest + "%s.dest" + * temp.path + and. > @ + (temp.moved dest).exists + temp.exists.not + +# Test. +[] > throws-on-opening-with-wrong-mode + tmpdir + .tmpfile + .open > @ + "x" + f > [f] + +# Test. +[] > throws-on-reading-from-not-existed-file + tmpdir + .tmpfile + .deleted + .open > @ + "r" + f > [f] + +# Test. +[] > throws-on-reading-or-writing-from-not-existed-file + tmpdir + .tmpfile + .deleted + .open > @ + "r+" + f > [f] + +# Test +[] > touches-absent-file-on-opening-for-writing + tmpdir + .tmpfile + .deleted + .open + "w" + true > [f] + .exists > @ + +# Test +[] > touches-absent-file-on-opening-for-appending + tmpdir + .tmpfile + .deleted + .open + "a" + true > [f] + .exists > @ + +# Test +[] > touches-absent-file-on-opening-for-writing-or-reading + tmpdir + .tmpfile + .deleted + .open + "w+" + true > [f] + .exists > @ + +# Test +[] > touches-absent-file-on-opening-for-reading-or-appending + tmpdir + .tmpfile + .deleted + .open + "a+" + true > [f] + .exists > @ + +# Test. +[] > writes-data-to-file + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .size + .eq 12 > @ + +# Test. +[] > appending-data-to-file + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open + "a" + f.write "!" > [f] + .size + .eq 13 > @ + +# Test. +[] > truncates-file-opened-for-writing + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open + "w" + true > [f] + .size + .eq 0 > @ + +# Test. +[] > truncates-file-opened-for-writing-or-reading + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open + "w+" + true > [f] + .size + .eq 0 > @ + +# Test. +[] > does-not-truncate-file-opened-for-appending + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open + "a" + true > [f] + .size + .eq 12 > @ + +# Test. +[] > does-not-truncate-file-opened-for-reading-or-appending + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open + "a+" + true > [f] + .size + .eq 12 > @ + +# Test. +[] > does-not-truncate-file-opened-for-reading + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open + "r" + true > [f] + .size + .eq 12 > @ + +# Test. +[] > throws-on-writing-with-wrong-mode + tmpdir + .tmpfile + .open > @ + "r" + f.write "Hello" > [f] + +# Test. +[] > reads-from-file + malloc + .of + 12 + [m] + tmpdir + .tmpfile + .open + "w" + f.write "Hello, world" > [f] + .open > @ + "r" + [f] >> + m.put > @ + f.read 12 + .eq "Hello, world" > @ + +# Test. +[] > throws-on-reading-from-file-with-wrong-mode + tmpdir + .tmpfile + .open > @ + "w" + f.read 12 > [f] + +# Test. +[] > reads-from-file-from-different-instances + tmpdir.tmpfile > temp + temp.path > src + seq > @ + * + temp + .open + "w" + f.write "Shrek is love" > [f] + malloc + .of + 13 + [m] >> + file + ^.src + .open > @ + "r" + [f] >> + ^.m.put > @ + f.read 13 + .eq "Shrek is love" + +# Test. +[] > writes-to-file-from-different-instances + tmpdir.tmpfile > temp + temp.path > src + seq > @ + * + temp + .open + "w" + f.write "Shrek is love" > [f] + malloc + .of + 14 + [m] >> + file + ^.src + .open > @ + "a+" + [f] >> + seq > @ + * + f.write "!" + ^.m.put + f.read 14 + .eq "Shrek is love!" + +# Test. +[] > reads-from-file-sequentially + malloc + .of + 5 + [m] + tmpdir + .tmpfile + .open > @ + "w+" + [f] >> + f.read 7 > i1 + i1.read 5 > i2 + seq > @ + * + f.write "Hello, world" + ^.m.put i2 + .eq "world" > @ + +# Test. +[] > writes-to-file-sequentially + tmpdir + .tmpfile + .open + "a+" + [f] + f.write "Hello, world" > o1 + o1.write "!" > @ + .size + .eq 13 > @ diff --git a/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo new file mode 100644 index 0000000000..45d7963220 --- /dev/null +++ b/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo @@ -0,0 +1,40 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016-2024 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + ++alias org.eolang.fs.tmpdir ++architect yegor256@gmail.com ++home https://github.com/objectionary/eo ++tests ++package org.eolang.fs ++version 0.0.0 + +# Test. +tmpdir.exists > [] > global-temp-dir-exists + +# Test. +tmpdir.is-directory > [] > global-temp-dir-is-directory + +# Test. +tmpdir.path.eq tmpdir.path > [] > returns-the-same-tmpdir + +# Test. +tmpdir.tmpfile.exists > [] > creates-tmpfile diff --git a/eo-runtime/src/test/eo/org/eolang/sys/uname-test.eo b/eo-runtime/src/test/eo/org/eolang/sys/os-test.eo similarity index 93% rename from eo-runtime/src/test/eo/org/eolang/sys/uname-test.eo rename to eo-runtime/src/test/eo/org/eolang/sys/os-test.eo index de20a4ef11..e5e91ec365 100644 --- a/eo-runtime/src/test/eo/org/eolang/sys/uname-test.eo +++ b/eo-runtime/src/test/eo/org/eolang/sys/os-test.eo @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -+alias org.eolang.sys.uname ++alias org.eolang.sys.os +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests @@ -31,6 +31,6 @@ [] > checks-os-family or. > @ or. - uname.is-windows - uname.is-macos - uname.is-linux + os.is-windows + os.is-macos + os.is-linux diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java new file mode 100644 index 0000000000..89c4cd0ec8 --- /dev/null +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java @@ -0,0 +1,4 @@ +package EOorg.EOeolang.EOfs; + +final class FilesTest { +} From 697638ca7dd602cfd45e33a986530e06d549f52c Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 10:33:43 +0300 Subject: [PATCH 02/33] feat(#3251): fix tests and style --- .codacy.yml | 24 +++- .../test/java/org/eolang/maven/FakeMaven.java | 1 + eo-runtime/src/main/eo/org/eolang/fs/dir.eo | 2 +- .../EOorg/EOeolang/EOfs/EOdir$EOwalk.java | 12 +- ...pen$EOfile_stream$EOread$EOread_bytes.java | 4 +- ...EOfile_stream$EOwrite$EOwritten_bytes.java | 4 +- .../EOfs/EOfile$EOtouched$EOtouch.java | 2 +- .../main/java/EOorg/EOeolang/EOfs/Files.java | 13 +- .../EOorg/EOeolang/EOsys/EOos$EOname.java | 2 +- .../src/test/eo/org/eolang/fs/file-tests.eo | 3 +- .../test/java/EOorg/EOeolang/CagesTest.java | 2 +- .../java/EOorg/EOeolang/EOfs/FilesTest.java | 136 ++++++++++++++++++ .../EOorg/EOeolang/EOfs/package-info.java | 34 +++++ .../java/EOorg/EOeolang/EOmallocTest.java | 2 +- .../java/EOorg/EOeolang/EOnumberTest.java | 2 +- .../test/java/EOorg/EOeolang/EOtryTest.java | 2 +- .../test/java/EOorg/EOeolang/HeapsTest.java | 2 +- 17 files changed, 219 insertions(+), 28 deletions(-) create mode 100644 eo-runtime/src/test/java/EOorg/EOeolang/EOfs/package-info.java diff --git a/.codacy.yml b/.codacy.yml index 3b45ebb673..c11c7904e7 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -25,10 +25,20 @@ # package name contains capital letter and such names are conventional. --- exclude_paths: - - "eo-runtime/src/main/java/EOorg/EOeolang/EOio/EOconsole$EOread$EOread_bytes.java" - - "eo-runtime/src/main/java/EOorg/EOeolang/EOio/EOconsole$EOwrite$EOwritten_bytes.java" - - "eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOconsoleTest.java" - - "eo-runtime/src/test/java/EOorg/EOeolang/EOsys/EOuname$EOφ.java" - - "eo-runtime/src/test/java/EOorg/EOeolang/EOsys/package-info.java" - - "eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java" - - "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/package-info.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOmade$EOmkdir.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOtmpfile$EOtouch.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOdeleted$EOdelete.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOwritten_bytes.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOprocess_file.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOsize.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java" + - "eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java" diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java index adec2b93e1..67669ae44d 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java @@ -195,6 +195,7 @@ public TjSmart foreign() { * @return Workspace after executing Mojo. * @throws java.io.IOException If some problem with filesystem has happened. * @checkstyle ExecutableStatementCountCheck (100 lines) + * @checkstyle JavaNCSSCheck (100 lines) */ public FakeMaven execute(final Class mojo) throws IOException { if (this.defaults) { diff --git a/eo-runtime/src/main/eo/org/eolang/fs/dir.eo b/eo-runtime/src/main/eo/org/eolang/fs/dir.eo index 453e84f34d..3077d23dfc 100644 --- a/eo-runtime/src/main/eo/org/eolang/fs/dir.eo +++ b/eo-runtime/src/main/eo/org/eolang/fs/dir.eo @@ -90,7 +90,7 @@ [] > tmpfile if. > @ ^.exists - QQ.fs.file touched.as-bytes.as-string + QQ.fs.file touch.as-bytes.as-string error sprintf "Directory %s does not exist, can't create temporary file" diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java index 85b9497154..17a58257d1 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOdir$EOwalk.java @@ -74,11 +74,13 @@ public Phi lambda() throws Exception { .map(p -> p.toAbsolutePath().toString()) .map(p -> p.substring(p.indexOf(path.toString()))) .filter(p -> matcher.matches(Paths.get(p))) - .map(p -> { - final Phi file = Phi.Φ.take("org.eolang.fs.file").copy(); - file.put(0, new ToPhi(p)); - return file; - }) + .map( + p -> { + final Phi file = Phi.Φ.take("org.eolang.fs.file").copy(); + file.put(0, new ToPhi(p)); + return file; + } + ) .toArray(Phi[]::new) ); } diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java index 2fcf7c2ea4..3fcaec4c78 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java @@ -44,7 +44,9 @@ * @checkstyle TypeNameCheck (100 lines) */ @XmirObject(oname = "file.open.file-stream.read.read-bytes") -public final class EOfile$EOopen$EOfile_stream$EOread$EOread_bytes extends PhDefault implements Atom { +public final class EOfile$EOopen$EOfile_stream$EOread$EOread_bytes + extends PhDefault + implements Atom { /** * Ctor. diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java index 517da88ef5..e07418650a 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java @@ -44,7 +44,9 @@ * @checkstyle TypeNameCheck (100 lines) */ @XmirObject(oname = "file.open.file-stream.write.written-bytes") -public final class EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes extends PhDefault implements Atom { +public final class EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes + extends PhDefault + implements Atom { /** * Ctor. diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java index 46bab632be..70d5952c32 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOtouched$EOtouch.java @@ -43,7 +43,7 @@ * @checkstyle TypeNameCheck (100 lines) */ @XmirObject(oname = "file.touched.touch") -public class EOfile$EOtouched$EOtouch extends PhDefault implements Atom { +public final class EOfile$EOtouched$EOtouch extends PhDefault implements Atom { @Override public Phi lambda() throws Exception { return new Data.ToPhi( diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java index 01a93b21a1..b713837d7d 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java @@ -45,7 +45,7 @@ @Versionized final class Files { /** - * Files instance + * Files instance. */ static final Files INSTANCE = new Files(); @@ -67,10 +67,13 @@ private Files() { * @throws FileNotFoundException If can't open file */ void open(final String name) throws FileNotFoundException { - this.streams.putIfAbsent(name, new Object[] { - new FileInputStream(name), - new FileOutputStream(name, true) - }); + this.streams.putIfAbsent( + name, + new Object[] { + new FileInputStream(name), + new FileOutputStream(name, true), + } + ); } /** diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java index c4c7faeb22..ddded9b97b 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java @@ -34,7 +34,7 @@ import org.eolang.XmirObject; /** - * Os.name + * Os.name. * @since 0.40 * @checkstyle TypeNameCheck (5 lines) */ diff --git a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo index 8217d3ab22..604ac106e8 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. ++alias org.eolang.fs.dir +alias org.eolang.fs.file +alias org.eolang.fs.tmpdir +alias org.eolang.txt.sprintf @@ -80,7 +81,7 @@ # Test. [] > resolves-and-touches - tmpdir.resolved "foo/bar" > resolved + dir (tmpdir.resolved "foo/bar") > resolved resolved.tmpfile > f seq > @ * diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java index 17a2523a1d..434cd5dcfc 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java @@ -41,7 +41,7 @@ * Test cases for {@link Cages}. * @since 0.36.0 */ -class CagesTest { +final class CagesTest { @Test void initializesObjectForTheFirstTime() { final Phi phi = new PhFake(); diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java index 89c4cd0ec8..d883984155 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java @@ -1,4 +1,140 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (10 lines) + */ package EOorg.EOeolang.EOfs; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.eolang.ExFailure; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Test case for {@link Files}. + * @since 0.40 + */ final class FilesTest { + /** + * Files instance. + */ + private static final Files INSTANCE = Files.INSTANCE; + + @Test + void opensWithoutErrors(@TempDir final Path dir) { + Assertions.assertDoesNotThrow( + () -> FilesTest.INSTANCE.open(FilesTest.tempFile(dir)), + "File should be opened without errors" + ); + } + + @Test + void throwsOnReadingWithoutOpening(@TempDir final Path dir) { + Assertions.assertThrows( + ExFailure.class, + () -> FilesTest.INSTANCE.read( + FilesTest.tempFile(dir), 10 + ), + "File should not allow to read before opening" + ); + } + + @Test + void throwsOnWritingWithoutOpening(@TempDir final Path dir) { + Assertions.assertThrows( + ExFailure.class, + () -> FilesTest.INSTANCE.write( + FilesTest.tempFile(dir), new byte[]{0x01} + ), + "File should not allow to write before opening" + ); + } + + @Test + void throwsOnClosingWithoutOpening(@TempDir final Path dir) { + Assertions.assertThrows( + ExFailure.class, + () -> FilesTest.INSTANCE.close( + FilesTest.tempFile(dir) + ), + "File should not allow to close before opening" + ); + } + + @Test + void readsFromFile(@TempDir final Path dir) throws IOException { + final String file = FilesTest.tempFile(dir); + final FileWriter writer = new FileWriter( + Paths.get(file).toFile() + ); + writer.write("Hello, world"); + writer.close(); + FilesTest.INSTANCE.open(file); + MatcherAssert.assertThat( + "The string should have been read from file", + FilesTest.INSTANCE.read(file, 12), + Matchers.equalTo("Hello, world".getBytes(StandardCharsets.UTF_8)) + ); + FilesTest.INSTANCE.close(file); + } + + @Test + void writesToFile(@TempDir final Path dir) throws IOException { + final String file = FilesTest.tempFile(dir); + final FileWriter writer = new FileWriter( + Paths.get(file).toFile() + ); + writer.write("Hello, world"); + writer.close(); + FilesTest.INSTANCE.open(file); + FilesTest.INSTANCE.write(file, "!".getBytes(StandardCharsets.UTF_8)); + MatcherAssert.assertThat( + "The string should have been read from file", + FilesTest.INSTANCE.read(file, 13), + Matchers.equalTo("Hello, world!".getBytes(StandardCharsets.UTF_8)) + ); + FilesTest.INSTANCE.close(file); + } + + /** + * Creates temporary file in directory. + * @param dir Directory + * @return Absolute path to temp file + * @throws IOException If fails to create temp file + */ + private static String tempFile(final Path dir) throws IOException { + final Path file = java.nio.file.Files.createTempFile(dir, null, null); + file.toFile().deleteOnExit(); + return file.toAbsolutePath().toString(); + } } diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/package-info.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/package-info.java new file mode 100644 index 0000000000..ccc58fa79d --- /dev/null +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/package-info.java @@ -0,0 +1,34 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (20 lines) + */ + +/** + * EO-fs, tests. + * + * @since 0.40 + */ +package EOorg.EOeolang.EOfs; diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOmallocTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOmallocTest.java index b55975bdfb..4c27293bdb 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOmallocTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOmallocTest.java @@ -47,7 +47,7 @@ * @since 0.1 */ @SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass") -public final class EOmallocTest { +final class EOmallocTest { @Test void freesMemory() { final Dummy dummy = new Dummy(); diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java index 930a3231d7..962f8f20c1 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java @@ -53,7 +53,7 @@ * @checkstyle TypeNameCheck (4 lines) */ @SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass") -public final class EOnumberTest { +final class EOnumberTest { @Test void hasDifferentHashes() { diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java index 0b092a504a..d6a57666ac 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOtryTest.java @@ -46,7 +46,7 @@ * * @since 0.19 */ -public final class EOtryTest { +final class EOtryTest { @Test public void catchesException() { diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java index 0172480a2a..da24db572f 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java @@ -41,7 +41,7 @@ * * @since 0.19 */ -public final class HeapsTest { +final class HeapsTest { /** * Heaps. */ From 3d1e8346f3fbb3c5f229f52fef9960b5bf02abe2 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 10:49:58 +0300 Subject: [PATCH 03/33] feat(#3251): custom config --- .github/.typos.toml | 3 +++ .github/workflows/typos.yml | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 .github/.typos.toml diff --git a/.github/.typos.toml b/.github/.typos.toml new file mode 100644 index 0000000000..2b84425c46 --- /dev/null +++ b/.github/.typos.toml @@ -0,0 +1,3 @@ +[default.extend-identifiers] +# *sigh* this just isn't worth the cost of fixing +EOfileEOis_directory = "EOfile$EOis_directory" \ No newline at end of file diff --git a/.github/workflows/typos.yml b/.github/workflows/typos.yml index 58db369c18..05b3e4ceb0 100644 --- a/.github/workflows/typos.yml +++ b/.github/workflows/typos.yml @@ -35,3 +35,5 @@ jobs: steps: - uses: actions/checkout@v4 - uses: crate-ci/typos@master + with: + config: ../.typos.toml From 63281e39df2ac80e08df7613a2d28877983532fa Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 10:51:19 +0300 Subject: [PATCH 04/33] feat(#3251): custom config x2 --- .github/workflows/typos.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/typos.yml b/.github/workflows/typos.yml index 05b3e4ceb0..1e17a8e3f2 100644 --- a/.github/workflows/typos.yml +++ b/.github/workflows/typos.yml @@ -36,4 +36,4 @@ jobs: - uses: actions/checkout@v4 - uses: crate-ci/typos@master with: - config: ../.typos.toml + config: .github/.typos.toml From fe1a2584e66144617214cdf75088b117e8e4cd3b Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 10:57:07 +0300 Subject: [PATCH 05/33] feat(#3251): custom config x3 --- .github/.typos.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/.typos.toml b/.github/.typos.toml index 2b84425c46..8e1239ad10 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1,3 +1,9 @@ +[default] +extend-ignore-identifiers-re = [ + # *sigh* this just isn't worth the cost of fixing + "EOfileEOis_directory", +] + [default.extend-identifiers] # *sigh* this just isn't worth the cost of fixing EOfileEOis_directory = "EOfile$EOis_directory" \ No newline at end of file From f713c9ada2dbf6d2fe515dd715badbd9477a534d Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 10:59:59 +0300 Subject: [PATCH 06/33] feat(#3251): custom config x4 --- .github/.typos.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/.typos.toml b/.github/.typos.toml index 8e1239ad10..3ce4fbfff2 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1,7 +1,7 @@ [default] extend-ignore-identifiers-re = [ # *sigh* this just isn't worth the cost of fixing - "EOfileEOis_directory", + "EOfile$EOis_directory", ] [default.extend-identifiers] From f8acb757d3cc89bf25ae2df499aa1ab923e7b7c8 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:00:54 +0300 Subject: [PATCH 07/33] feat(#3251): custom config x5 --- .github/workflows/typos.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/typos.yml b/.github/workflows/typos.yml index 1e17a8e3f2..58db369c18 100644 --- a/.github/workflows/typos.yml +++ b/.github/workflows/typos.yml @@ -35,5 +35,3 @@ jobs: steps: - uses: actions/checkout@v4 - uses: crate-ci/typos@master - with: - config: .github/.typos.toml From 30b56658311f75a7ce5c86611232ac181777339c Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:04:40 +0300 Subject: [PATCH 08/33] feat(#3251): custom config x6 --- .github/.typos.toml | 9 +-------- .github/workflows/typos.yml | 2 ++ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/.github/.typos.toml b/.github/.typos.toml index 3ce4fbfff2..a4a969a8c9 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1,9 +1,2 @@ -[default] -extend-ignore-identifiers-re = [ - # *sigh* this just isn't worth the cost of fixing - "EOfile$EOis_directory", -] - [default.extend-identifiers] -# *sigh* this just isn't worth the cost of fixing -EOfileEOis_directory = "EOfile$EOis_directory" \ No newline at end of file +EOfileEOis_directory = "EOfile$EOis_directory" diff --git a/.github/workflows/typos.yml b/.github/workflows/typos.yml index 58db369c18..b285e788a1 100644 --- a/.github/workflows/typos.yml +++ b/.github/workflows/typos.yml @@ -35,3 +35,5 @@ jobs: steps: - uses: actions/checkout@v4 - uses: crate-ci/typos@master + with: + config: ./.github/.typos.toml From 07c8f865f913e78f371552ba22dd0b6e768f48f8 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:05:48 +0300 Subject: [PATCH 09/33] feat(#3251): custom config x7 --- .github/.typos.toml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/.typos.toml b/.github/.typos.toml index a4a969a8c9..a4411e910a 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1,2 +1,10 @@ +[default] +extend-ignore-identifiers-re = [ + # *sigh* this just isn't worth the cost of fixing + "EOfile$EOis_directory", + "fid" +] + [default.extend-identifiers] -EOfileEOis_directory = "EOfile$EOis_directory" +# *sigh* this just isn't worth the cost of fixing +fid = "EOfile$EOis_directory" \ No newline at end of file From 9ccf1c10c2c8ba743840bebf7b5110e1fbe55832 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:08:10 +0300 Subject: [PATCH 10/33] feat(#3251): custom config x8 --- .github/.typos.toml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/.github/.typos.toml b/.github/.typos.toml index a4411e910a..2242acd60a 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1,10 +1,2 @@ -[default] -extend-ignore-identifiers-re = [ - # *sigh* this just isn't worth the cost of fixing - "EOfile$EOis_directory", - "fid" -] - -[default.extend-identifiers] -# *sigh* this just isn't worth the cost of fixing -fid = "EOfile$EOis_directory" \ No newline at end of file +[default.extend-words] +fileisdir = "EOfile$EOis_directory" \ No newline at end of file From f4f7a582ba9499575e78861f8dbac0cdc4afe25a Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:09:24 +0300 Subject: [PATCH 11/33] feat(#3251): custom config x9 --- .github/.typos.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/.typos.toml b/.github/.typos.toml index 2242acd60a..44b9219f36 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1,2 +1 @@ -[default.extend-words] -fileisdir = "EOfile$EOis_directory" \ No newline at end of file +extend-exclude = ["eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java"] \ No newline at end of file From 21f8d2d63c523ebec3af7874a75d3e55e59cc464 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:10:23 +0300 Subject: [PATCH 12/33] feat(#3251): custom config x10 --- .github/.typos.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/.typos.toml b/.github/.typos.toml index 44b9219f36..4ead95e0a2 100644 --- a/.github/.typos.toml +++ b/.github/.typos.toml @@ -1 +1,2 @@ +[files] extend-exclude = ["eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java"] \ No newline at end of file From 4a31115508e9a446085546425d59906425cf68b7 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:23:27 +0300 Subject: [PATCH 13/33] feat(#3251): codacy --- .codacy.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.codacy.yml b/.codacy.yml index c11c7904e7..dfe5e82d0b 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -32,8 +32,9 @@ exclude_paths: - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java" + - "eo-runtime/src/test/java/EOorg/EOeolang/EOfs/package-info.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java" - - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOwritten_bytes.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOprocess_file.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOsize.java" From 9f2efccbaaeb7d3bcb22b7657bf598dea47d6ad7 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 11:48:22 +0300 Subject: [PATCH 14/33] feat(#3251): more codacy --- .codacy.yml | 1 + eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo | 8 +------- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.codacy.yml b/.codacy.yml index dfe5e82d0b..25ec095124 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -32,6 +32,7 @@ exclude_paths: - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOexists.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOis_directory.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOmoved$EOmove.java" + - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/package-info.java" - "eo-runtime/src/test/java/EOorg/EOeolang/EOfs/package-info.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOwrite$EOwritten_bytes.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOopen$EOfile_stream$EOread$EOread_bytes.java" diff --git a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo index 604ac106e8..f6e01c65ae 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo @@ -69,12 +69,6 @@ [] > does-not-fail-on-double-deleting tmpdir.tmpfile.deleted.deleted.exists.not > @ -# Test. -[] > resolves-in-a-right-way - eq. > @ - (tmpdir.resolved "foo/bar").path - tmpdir.path.concat "foo/bar" - # Test. [] > throws-an-error-on-touching-temp-file-in-absent-dir (tmpdir.resolve "foo").tmpfile > @ @@ -90,7 +84,7 @@ f.exists contains. text f.path - "/foo/bar" + "bar" # Test. [] > moves-a-file From 9ccb6534b3a030093974a9e829bda30a3d9bfcef Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 12:50:46 +0300 Subject: [PATCH 15/33] feat(#3251): temp on windows --- .github/workflows/mvn.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index 854bc6f863..b99f237442 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -54,6 +54,11 @@ jobs: path: ~/.m2/repository key: ${{ runner.os }}-jdk-${{ matrix.java }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-jdk-${{ matrix.java }}-maven- + - name: Temp dirs + if: matrix.os == 'windows-2022' + run: | + mkdir -p ${{ runner.temp }}/example_temp_dir + dir ${{ runner.temp }} # The next step is required to avoid some exceptions that occur # when running on Windows OS. For example like this one: # - https://github.com/objectionary/eo/issues/2336 From 48e7a9fb3e010f4f684325ce35e831d55515ed0b Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 12:53:06 +0300 Subject: [PATCH 16/33] feat(#3251): prints tmpdir --- eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo index 45d7963220..708e2d9ce3 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo @@ -21,12 +21,16 @@ # SOFTWARE. +alias org.eolang.fs.tmpdir ++alias org.eolang.io.stdout +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests +package org.eolang.fs +version 0.0.0 +# Test. +stdout tmpdir > [] > prints-tmpdir + # Test. tmpdir.exists > [] > global-temp-dir-exists From 8588e7714ccbc7fff7e0b2151713b7501da58c40 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 12:56:21 +0300 Subject: [PATCH 17/33] feat(#3251): prints tmpdir x2 --- eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo index 708e2d9ce3..991fda5778 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/tmpdir-tests.eo @@ -22,6 +22,7 @@ +alias org.eolang.fs.tmpdir +alias org.eolang.io.stdout ++alias org.eolang.txt.sprintf +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests @@ -29,7 +30,11 @@ +version 0.0.0 # Test. -stdout tmpdir > [] > prints-tmpdir +[] > prints-tmpdir + stdout > @ + sprintf + "Temp dir is %s\n" + * tmpdir # Test. tmpdir.exists > [] > global-temp-dir-exists From 7c0c0a7f145b510e2c2c5f0883ef6e8bab829c4e Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 18:11:21 +0300 Subject: [PATCH 18/33] feat(#3251): env is --- eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java | 1 + 1 file changed, 1 insertion(+) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java index 92446d50d5..26d0291280 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java @@ -54,6 +54,7 @@ public Phi lambda() throws Exception { final String env = System.getenv( new Dataized(this.take("name")).asString() ); + System.out.println("ENV is " + env); final Phi var; if (env != null) { var = new Data.ToPhi(env); From 5c65d8e84d6f6dcf1f989211c4607c1ba367d3db Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 18:31:19 +0300 Subject: [PATCH 19/33] feat(#3251): env is x2 --- .github/workflows/mvn.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index b99f237442..606324a11b 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -38,7 +38,7 @@ jobs: name: mvn strategy: matrix: - os: [ ubuntu-20.04, windows-2022, macos-12 ] + os: [ windows-2022 ] java: [ 11, 20 ] runs-on: ${{ matrix.os }} env: From 5940e21625b18b60e7000cbc108ca86fa8082fc6 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 18:51:52 +0300 Subject: [PATCH 20/33] feat(#3251): fix unescape --- .github/workflows/mvn.yml | 7 +- .../java/EOorg/EOeolang/EOsys/EOgetenv.java | 1 - eo-runtime/src/main/java/org/eolang/Data.java | 104 +++++++----------- 3 files changed, 41 insertions(+), 71 deletions(-) diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index 606324a11b..854bc6f863 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -38,7 +38,7 @@ jobs: name: mvn strategy: matrix: - os: [ windows-2022 ] + os: [ ubuntu-20.04, windows-2022, macos-12 ] java: [ 11, 20 ] runs-on: ${{ matrix.os }} env: @@ -54,11 +54,6 @@ jobs: path: ~/.m2/repository key: ${{ runner.os }}-jdk-${{ matrix.java }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-jdk-${{ matrix.java }}-maven- - - name: Temp dirs - if: matrix.os == 'windows-2022' - run: | - mkdir -p ${{ runner.temp }}/example_temp_dir - dir ${{ runner.temp }} # The next step is required to avoid some exceptions that occur # when running on Windows OS. For example like this one: # - https://github.com/objectionary/eo/issues/2336 diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java index 26d0291280..92446d50d5 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java @@ -54,7 +54,6 @@ public Phi lambda() throws Exception { final String env = System.getenv( new Dataized(this.take("name")).asString() ); - System.out.println("ENV is " + env); final Phi var; if (env != null) { var = new Data.ToPhi(env); diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 4f5c2230b6..02b71df1df 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -208,92 +208,68 @@ private static Phi toPhi(final Object obj) { * removed as a result of refactoring. * @checkstyle CyclomaticComplexityCheck (100 lines) * @checkstyle JavaNCSSCheck (100 lines) - * @checkstyle NestedIfDepthCheck (100 lines) * @checkstyle ModifiedControlVariableCheck (100 lines) */ - @SuppressWarnings({ - "PMD.AvoidReassigningLoopVariables", - "PMD.CognitiveComplexity", - "PMD.NPathComplexity" - }) + @SuppressWarnings({"PMD.AvoidReassigningLoopVariables", "PMD.CognitiveComplexity"}) private static String unescapeJavaString(final String str) { - final StringBuilder unescaped = new StringBuilder(str.length()); + final StringBuilder unescaped = new StringBuilder(); for (int idx = 0; idx < str.length(); ++idx) { - char chr = str.charAt(idx); - if (chr == '\\') { - final char next; - if (idx == str.length() - 1) { - next = '\\'; - } else { - next = str.charAt(idx + 1); - } - if (next >= '0' && next <= '7') { - final StringBuilder code = new StringBuilder(String.valueOf(next)); - ++idx; - if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0' - && str.charAt(idx + 1) <= '7') { - code.append(str.charAt(idx + 1)); - ++idx; - if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0' - && str.charAt(idx + 1) <= '7') { - code.append(str.charAt(idx + 1)); - ++idx; - } - } - unescaped.append((char) Integer.parseInt(code.toString(), 8)); - continue; - } + final char current = str.charAt(idx); + if (current == '\\' && idx + 1 < str.length()) { + final char next = str.charAt(idx + 1); switch (next) { - case '\\': - break; case 'b': - chr = '\b'; + unescaped.append('\b'); + ++idx; break; - case 'f': - chr = '\f'; + case 't': + unescaped.append('\t'); + ++idx; break; case 'n': - chr = '\n'; + unescaped.append('\n'); + ++idx; break; - case 'r': - chr = '\r'; + case 'f': + unescaped.append('\f'); + ++idx; break; - case 't': - chr = '\t'; + case 'r': + unescaped.append('\r'); + ++idx; break; case '\"': - chr = '\"'; + unescaped.append('\"'); + ++idx; break; case '\'': - chr = '\''; + unescaped.append('\''); + ++idx; + break; + case '\\': + unescaped.append('\\'); + ++idx; break; case 'u': - if (idx >= str.length() - 5) { - chr = 'u'; - break; + if (idx + 5 < str.length()) { + final String hex = str.substring(idx + 2, idx + 6); + try { + unescaped.append((char) Integer.parseInt(hex, 16)); + idx += 5; + } catch (final NumberFormatException exception) { + unescaped.append(current); + } + } else { + unescaped.append(current); } - unescaped.append( - Character.toChars( - Integer.parseInt( - String.join( - "", - String.valueOf(str.charAt(idx + 2)), - String.valueOf(str.charAt(idx + 3)), - String.valueOf(str.charAt(idx + 4)), - String.valueOf(str.charAt(idx + 5)) - ), - 16 - ) - ) - ); - idx += 5; - continue; + break; default: + unescaped.append(current); break; } - ++idx; + } else { + unescaped.append(current); } - unescaped.append(chr); } return unescaped.toString(); } From 35a63dfc8362cf9825424af58d1abf1a826ca72e Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 18:57:17 +0300 Subject: [PATCH 21/33] feat(#3251): malloc-as-output --- eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo b/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo index bc529d2ac5..21fce98cd1 100644 --- a/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo +++ b/eo-runtime/src/main/eo/org/eolang/io/malloc-as-output.eo @@ -71,4 +71,4 @@ * ^.^.^.allocated.write offset buffer ^.^.output-block - offset.plus buffer.size \ No newline at end of file + offset.plus buffer.size From ea51ce5b868d305d63c1f4f72c6009583dcd0f41 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 20:00:24 +0300 Subject: [PATCH 22/33] feat(#3251): try unescape --- .github/workflows/mvn.yml | 4 ++-- eo-runtime/src/main/java/org/eolang/Data.java | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index 854bc6f863..8274381685 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -38,7 +38,7 @@ jobs: name: mvn strategy: matrix: - os: [ ubuntu-20.04, windows-2022, macos-12 ] + os: [ windows-2022 ] java: [ 11, 20 ] runs-on: ${{ matrix.os }} env: @@ -69,4 +69,4 @@ jobs: echo [http] >> %USERPROFILE%\.cargo\config.toml echo multiplexing = false >> %USERPROFILE%\.cargo\config.toml shell: cmd - - run: mvn clean install -Pqulice --errors --batch-mode + - run: mvn clean install -Pqulice --errors --batch-mode -rf :eo-runtime diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 02b71df1df..5ffceaff73 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -212,7 +212,7 @@ private static Phi toPhi(final Object obj) { */ @SuppressWarnings({"PMD.AvoidReassigningLoopVariables", "PMD.CognitiveComplexity"}) private static String unescapeJavaString(final String str) { - final StringBuilder unescaped = new StringBuilder(); + final StringBuilder unescaped = new StringBuilder(str.length()); for (int idx = 0; idx < str.length(); ++idx) { final char current = str.charAt(idx); if (current == '\\' && idx + 1 < str.length()) { @@ -238,7 +238,7 @@ private static String unescapeJavaString(final String str) { unescaped.append('\r'); ++idx; break; - case '\"': + case '"': unescaped.append('\"'); ++idx; break; @@ -257,14 +257,15 @@ private static String unescapeJavaString(final String str) { unescaped.append((char) Integer.parseInt(hex, 16)); idx += 5; } catch (final NumberFormatException exception) { - unescaped.append(current); + unescaped.append('\\').append('u'); } } else { - unescaped.append(current); + unescaped.append('\\').append('u'); } break; default: - unescaped.append(current); + unescaped.append('\\').append(next); + ++idx; break; } } else { From c8d4ae7772a6b7105e35e1a79fe3b35fd7d0db08 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 20:06:23 +0300 Subject: [PATCH 23/33] feat(#3251): try unescape x2 --- .github/workflows/mvn.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index 8274381685..5f6ce245fe 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -69,4 +69,4 @@ jobs: echo [http] >> %USERPROFILE%\.cargo\config.toml echo multiplexing = false >> %USERPROFILE%\.cargo\config.toml shell: cmd - - run: mvn clean install -Pqulice --errors --batch-mode -rf :eo-runtime + - run: mvn clean install -Pqulice --errors --batch-mode From 9ef3eae189a8a6cdd42e6c249222ceb4a65a346d Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 22:09:54 +0300 Subject: [PATCH 24/33] feat(#3251): replace slashes --- .../EOfs/EOfile$EOresolved$EOresolve.java | 1 + eo-runtime/src/main/java/org/eolang/Data.java | 105 +++++++++++------- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java index 3b85fa828a..bc96d3a5a9 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java @@ -51,6 +51,7 @@ public Phi lambda() throws Exception { Paths.get(new Dataized(rho.take(Attr.RHO).take("path")).asString()) .resolve(new Dataized(rho.take("other")).asString()) .toString() + .replace("\\", "\\\\") ); } } diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 5ffceaff73..4f5c2230b6 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -208,69 +208,92 @@ private static Phi toPhi(final Object obj) { * removed as a result of refactoring. * @checkstyle CyclomaticComplexityCheck (100 lines) * @checkstyle JavaNCSSCheck (100 lines) + * @checkstyle NestedIfDepthCheck (100 lines) * @checkstyle ModifiedControlVariableCheck (100 lines) */ - @SuppressWarnings({"PMD.AvoidReassigningLoopVariables", "PMD.CognitiveComplexity"}) + @SuppressWarnings({ + "PMD.AvoidReassigningLoopVariables", + "PMD.CognitiveComplexity", + "PMD.NPathComplexity" + }) private static String unescapeJavaString(final String str) { final StringBuilder unescaped = new StringBuilder(str.length()); for (int idx = 0; idx < str.length(); ++idx) { - final char current = str.charAt(idx); - if (current == '\\' && idx + 1 < str.length()) { - final char next = str.charAt(idx + 1); + char chr = str.charAt(idx); + if (chr == '\\') { + final char next; + if (idx == str.length() - 1) { + next = '\\'; + } else { + next = str.charAt(idx + 1); + } + if (next >= '0' && next <= '7') { + final StringBuilder code = new StringBuilder(String.valueOf(next)); + ++idx; + if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0' + && str.charAt(idx + 1) <= '7') { + code.append(str.charAt(idx + 1)); + ++idx; + if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0' + && str.charAt(idx + 1) <= '7') { + code.append(str.charAt(idx + 1)); + ++idx; + } + } + unescaped.append((char) Integer.parseInt(code.toString(), 8)); + continue; + } switch (next) { + case '\\': + break; case 'b': - unescaped.append('\b'); - ++idx; + chr = '\b'; break; - case 't': - unescaped.append('\t'); - ++idx; + case 'f': + chr = '\f'; break; case 'n': - unescaped.append('\n'); - ++idx; - break; - case 'f': - unescaped.append('\f'); - ++idx; + chr = '\n'; break; case 'r': - unescaped.append('\r'); - ++idx; + chr = '\r'; break; - case '"': - unescaped.append('\"'); - ++idx; + case 't': + chr = '\t'; break; - case '\'': - unescaped.append('\''); - ++idx; + case '\"': + chr = '\"'; break; - case '\\': - unescaped.append('\\'); - ++idx; + case '\'': + chr = '\''; break; case 'u': - if (idx + 5 < str.length()) { - final String hex = str.substring(idx + 2, idx + 6); - try { - unescaped.append((char) Integer.parseInt(hex, 16)); - idx += 5; - } catch (final NumberFormatException exception) { - unescaped.append('\\').append('u'); - } - } else { - unescaped.append('\\').append('u'); + if (idx >= str.length() - 5) { + chr = 'u'; + break; } - break; + unescaped.append( + Character.toChars( + Integer.parseInt( + String.join( + "", + String.valueOf(str.charAt(idx + 2)), + String.valueOf(str.charAt(idx + 3)), + String.valueOf(str.charAt(idx + 4)), + String.valueOf(str.charAt(idx + 5)) + ), + 16 + ) + ) + ); + idx += 5; + continue; default: - unescaped.append('\\').append(next); - ++idx; break; } - } else { - unescaped.append(current); + ++idx; } + unescaped.append(chr); } return unescaped.toString(); } From 00a0f742bb00b2d9ef44c94d5bf173fb9d6be50a Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 22:39:36 +0300 Subject: [PATCH 25/33] feat(#3251): unescape --- eo-runtime/src/main/java/org/eolang/Data.java | 104 +++++++----------- 1 file changed, 40 insertions(+), 64 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 4f5c2230b6..02b71df1df 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -208,92 +208,68 @@ private static Phi toPhi(final Object obj) { * removed as a result of refactoring. * @checkstyle CyclomaticComplexityCheck (100 lines) * @checkstyle JavaNCSSCheck (100 lines) - * @checkstyle NestedIfDepthCheck (100 lines) * @checkstyle ModifiedControlVariableCheck (100 lines) */ - @SuppressWarnings({ - "PMD.AvoidReassigningLoopVariables", - "PMD.CognitiveComplexity", - "PMD.NPathComplexity" - }) + @SuppressWarnings({"PMD.AvoidReassigningLoopVariables", "PMD.CognitiveComplexity"}) private static String unescapeJavaString(final String str) { - final StringBuilder unescaped = new StringBuilder(str.length()); + final StringBuilder unescaped = new StringBuilder(); for (int idx = 0; idx < str.length(); ++idx) { - char chr = str.charAt(idx); - if (chr == '\\') { - final char next; - if (idx == str.length() - 1) { - next = '\\'; - } else { - next = str.charAt(idx + 1); - } - if (next >= '0' && next <= '7') { - final StringBuilder code = new StringBuilder(String.valueOf(next)); - ++idx; - if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0' - && str.charAt(idx + 1) <= '7') { - code.append(str.charAt(idx + 1)); - ++idx; - if (idx < str.length() - 1 && str.charAt(idx + 1) >= '0' - && str.charAt(idx + 1) <= '7') { - code.append(str.charAt(idx + 1)); - ++idx; - } - } - unescaped.append((char) Integer.parseInt(code.toString(), 8)); - continue; - } + final char current = str.charAt(idx); + if (current == '\\' && idx + 1 < str.length()) { + final char next = str.charAt(idx + 1); switch (next) { - case '\\': - break; case 'b': - chr = '\b'; + unescaped.append('\b'); + ++idx; break; - case 'f': - chr = '\f'; + case 't': + unescaped.append('\t'); + ++idx; break; case 'n': - chr = '\n'; + unescaped.append('\n'); + ++idx; break; - case 'r': - chr = '\r'; + case 'f': + unescaped.append('\f'); + ++idx; break; - case 't': - chr = '\t'; + case 'r': + unescaped.append('\r'); + ++idx; break; case '\"': - chr = '\"'; + unescaped.append('\"'); + ++idx; break; case '\'': - chr = '\''; + unescaped.append('\''); + ++idx; + break; + case '\\': + unescaped.append('\\'); + ++idx; break; case 'u': - if (idx >= str.length() - 5) { - chr = 'u'; - break; + if (idx + 5 < str.length()) { + final String hex = str.substring(idx + 2, idx + 6); + try { + unescaped.append((char) Integer.parseInt(hex, 16)); + idx += 5; + } catch (final NumberFormatException exception) { + unescaped.append(current); + } + } else { + unescaped.append(current); } - unescaped.append( - Character.toChars( - Integer.parseInt( - String.join( - "", - String.valueOf(str.charAt(idx + 2)), - String.valueOf(str.charAt(idx + 3)), - String.valueOf(str.charAt(idx + 4)), - String.valueOf(str.charAt(idx + 5)) - ), - 16 - ) - ) - ); - idx += 5; - continue; + break; default: + unescaped.append(current); break; } - ++idx; + } else { + unescaped.append(current); } - unescaped.append(chr); } return unescaped.toString(); } From eeb63f53b57cc3640899fd10d9718c2b264df7b8 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 23:14:35 +0300 Subject: [PATCH 26/33] feat(#3251): file test --- .../java/EOorg/EOeolang/EOfs/EOfileTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 eo-runtime/src/test/java/EOorg/EOeolang/EOfs/EOfileTest.java diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/EOfileTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/EOfileTest.java new file mode 100644 index 0000000000..bd5b6a5c63 --- /dev/null +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/EOfileTest.java @@ -0,0 +1,61 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (10 lines) + */ +package EOorg.EOeolang.EOfs; + +import java.nio.file.Path; +import org.eolang.Data; +import org.eolang.Dataized; +import org.eolang.PhWith; +import org.eolang.Phi; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Test case for {@link EOfile}. + * @since 0.40 + */ +@SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass") +final class EOfileTest { + @Test + void resolvesPath(@TempDir final Path temp) { + final Phi resolved = new PhWith( + new EOfile(), + "path", new Data.ToPhi(temp.toAbsolutePath().toString()) + ).take("resolved").copy(); + resolved.put(0, new Data.ToPhi("foo/bar/baz")); + MatcherAssert.assertThat( + "The `file.resolved` object should resolve in a right way", + new Dataized(resolved).asString(), + Matchers.equalTo( + temp.resolve("foo/bar/baz").toAbsolutePath().toString() + ) + ); + } +} From 9d4510f16381b68ae1144da6cd2f42016423138f Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Thu, 1 Aug 2024 23:47:38 +0300 Subject: [PATCH 27/33] feat(#3251): print --- .../java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java index bc96d3a5a9..d99aa0ae38 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java @@ -47,11 +47,13 @@ public final class EOfile$EOresolved$EOresolve extends PhDefault implements Atom @Override public Phi lambda() throws Exception { final Phi rho = this.take(Attr.RHO); - return new Data.ToPhi( + final Phi data = new Data.ToPhi( Paths.get(new Dataized(rho.take(Attr.RHO).take("path")).asString()) .resolve(new Dataized(rho.take("other")).asString()) .toString() .replace("\\", "\\\\") ); + System.out.printf("Resolved: %s%n", new Dataized(data).asString()); + return data; } } From 3a4a9c3a1d5ac5d86167ab2cb4d58af6aae9f206 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 2 Aug 2024 00:21:30 +0300 Subject: [PATCH 28/33] feat(#3251): dirs --- .../java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java | 2 ++ eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java index d99aa0ae38..62b9438b90 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java @@ -27,7 +27,9 @@ */ package EOorg.EOeolang.EOfs; +import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Arrays; import org.eolang.Atom; import org.eolang.Attr; import org.eolang.Data; diff --git a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo index 1762c4ce5f..94af5006c9 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo @@ -37,7 +37,7 @@ [] > makes-new-directory made. > d dir - tmpdir.tmpfile.deleted.resolved "foo" + tmpdir.tmpfile.deleted.resolved "new-dir" and. > @ d.exists d.is-directory @@ -46,7 +46,7 @@ [] > deletes-empty-directory dir tmpdir.tmpfile.deleted.resolved - "bar" + "empty" .made .deleted .exists From 6af99ac1985a6415b7d58dc35e55d0b42357a457 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 2 Aug 2024 00:23:33 +0300 Subject: [PATCH 29/33] feat(#3251): dirs x2 --- eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo index 94af5006c9..3e7d2f44f8 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo @@ -37,7 +37,7 @@ [] > makes-new-directory made. > d dir - tmpdir.tmpfile.deleted.resolved "new-dir" + tmpdir.tmpfile.deleted.resolved "foo-new" and. > @ d.exists d.is-directory @@ -46,7 +46,7 @@ [] > deletes-empty-directory dir tmpdir.tmpfile.deleted.resolved - "empty" + "bar-empty" .made .deleted .exists From 459743987c1ed4b35ff84d12ca945eab1cb782e7 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 2 Aug 2024 12:05:58 +0300 Subject: [PATCH 30/33] feat(#3251): disable some tests on windows --- .github/workflows/mvn.yml | 2 +- .../src/test/eo/org/eolang/fs/dir-test.eo | 24 ++++++++++++----- .../src/test/eo/org/eolang/fs/file-tests.eo | 27 +++++++++++++------ .../java/EOorg/EOeolang/EOfs/FilesTest.java | 9 ------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml index 5f6ce245fe..854bc6f863 100644 --- a/.github/workflows/mvn.yml +++ b/.github/workflows/mvn.yml @@ -38,7 +38,7 @@ jobs: name: mvn strategy: matrix: - os: [ windows-2022 ] + os: [ ubuntu-20.04, windows-2022, macos-12 ] java: [ 11, 20 ] runs-on: ${{ matrix.os }} env: diff --git a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo index 3e7d2f44f8..ded4369d2b 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo @@ -22,6 +22,7 @@ +alias org.eolang.fs.dir +alias org.eolang.fs.tmpdir ++alias org.eolang.sys.os +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests @@ -43,14 +44,23 @@ d.is-directory # Test. +# @todo #3251:30min Enable the test on windows. The test fails on windows +# because `file.resolved` object for some reason produces invalid path +# on windows with unreadable characters. It appends "bar" to origin path +# and in "your-path\bar" '\b' is understood as <0x08> (backspace) unreadable character. +# So the result path is invalid and does not work. Maybe problem is related to +# `unescapedJavaString` function in `org.eolang.Data.ToPhi` object. But it seems it +# works fine. [] > deletes-empty-directory - dir - tmpdir.tmpfile.deleted.resolved - "bar-empty" - .made - .deleted - .exists - .not > @ + or. > @ + os.is-windows + dir + tmpdir.tmpfile.deleted.resolved + "bar-empty" + .made + .deleted + .exists + .not # Test. [] > throws-on-opening-directory diff --git a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo index f6e01c65ae..febd63b6f4 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo @@ -25,6 +25,7 @@ +alias org.eolang.fs.tmpdir +alias org.eolang.txt.sprintf +alias org.eolang.txt.text ++alias org.eolang.sys.os +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests @@ -74,17 +75,27 @@ (tmpdir.resolve "foo").tmpfile > @ # Test. +# @todo #3251:30min Enable the test on windows. The test fails on windows +# because `file.resolved` object for some reason produces invalid path +# on windows with unreadable characters. It appends "foo" to origin path +# and in "your-path\foo\bar" '\f' is understood as <0x00c> (form freed) unreadable +# character, '\b' is understood as <0x08> (backspace) unreadable character. +# So the result path is invalid and does not work. Maybe problem is related to +# `unescapedJavaString` function in `org.eolang.Data.ToPhi` object. But it seems it +# works fine. [] > resolves-and-touches dir (tmpdir.resolved "foo/bar") > resolved resolved.tmpfile > f - seq > @ - * - resolved.deleted.made.path - and. - f.exists - contains. - text f.path - "bar" + or. > @ + os.is-windows + seq + * + resolved.deleted.made.path + and. + f.exists + contains. + text f.path + "bar" # Test. [] > moves-a-file diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java index d883984155..d07788c2a0 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java @@ -27,7 +27,6 @@ */ package EOorg.EOeolang.EOfs; -import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -50,14 +49,6 @@ final class FilesTest { */ private static final Files INSTANCE = Files.INSTANCE; - @Test - void opensWithoutErrors(@TempDir final Path dir) { - Assertions.assertDoesNotThrow( - () -> FilesTest.INSTANCE.open(FilesTest.tempFile(dir)), - "File should be opened without errors" - ); - } - @Test void throwsOnReadingWithoutOpening(@TempDir final Path dir) { Assertions.assertThrows( From f88a58bb09e94ed1ebd7e52784a5af79667a54dc Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 2 Aug 2024 12:07:13 +0300 Subject: [PATCH 31/33] feat(#3251): sout --- .codacy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.codacy.yml b/.codacy.yml index 25ec095124..a5a82ec30d 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -44,3 +44,4 @@ exclude_paths: - "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOgetenv.java" - "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/EOos$EOname.java" - "eo-runtime/src/test/java/EOorg/EOeolang/EOfs/FilesTest.java" + - "eo-runtime/src/test/java/EOorg/EOeolang/EOfs/EOfileTest.java" From 478ecf30b88d7cfc724159dd373bba265acf4543 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 2 Aug 2024 12:08:07 +0300 Subject: [PATCH 32/33] feat(#3251): resolve --- .../java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java index 62b9438b90..98ccdbc3a5 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java @@ -49,13 +49,11 @@ public final class EOfile$EOresolved$EOresolve extends PhDefault implements Atom @Override public Phi lambda() throws Exception { final Phi rho = this.take(Attr.RHO); - final Phi data = new Data.ToPhi( + return new Data.ToPhi( Paths.get(new Dataized(rho.take(Attr.RHO).take("path")).asString()) .resolve(new Dataized(rho.take("other")).asString()) .toString() .replace("\\", "\\\\") ); - System.out.printf("Resolved: %s%n", new Dataized(data).asString()); - return data; } } From 68924108de698fd613545db7a8f09d052c91a55c Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 2 Aug 2024 15:36:58 +0300 Subject: [PATCH 33/33] feat(#3251): fix --- .../EOfs/EOfile$EOresolved$EOresolve.java | 1 - .../src/test/eo/org/eolang/fs/dir-test.eo | 24 +++++------------ .../src/test/eo/org/eolang/fs/file-tests.eo | 27 ++++++------------- 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java index 98ccdbc3a5..b4c89adb45 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOfs/EOfile$EOresolved$EOresolve.java @@ -53,7 +53,6 @@ public Phi lambda() throws Exception { Paths.get(new Dataized(rho.take(Attr.RHO).take("path")).asString()) .resolve(new Dataized(rho.take("other")).asString()) .toString() - .replace("\\", "\\\\") ); } } diff --git a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo index ded4369d2b..3e7d2f44f8 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/dir-test.eo @@ -22,7 +22,6 @@ +alias org.eolang.fs.dir +alias org.eolang.fs.tmpdir -+alias org.eolang.sys.os +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests @@ -44,23 +43,14 @@ d.is-directory # Test. -# @todo #3251:30min Enable the test on windows. The test fails on windows -# because `file.resolved` object for some reason produces invalid path -# on windows with unreadable characters. It appends "bar" to origin path -# and in "your-path\bar" '\b' is understood as <0x08> (backspace) unreadable character. -# So the result path is invalid and does not work. Maybe problem is related to -# `unescapedJavaString` function in `org.eolang.Data.ToPhi` object. But it seems it -# works fine. [] > deletes-empty-directory - or. > @ - os.is-windows - dir - tmpdir.tmpfile.deleted.resolved - "bar-empty" - .made - .deleted - .exists - .not + dir + tmpdir.tmpfile.deleted.resolved + "bar-empty" + .made + .deleted + .exists + .not > @ # Test. [] > throws-on-opening-directory diff --git a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo index febd63b6f4..f6e01c65ae 100644 --- a/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/fs/file-tests.eo @@ -25,7 +25,6 @@ +alias org.eolang.fs.tmpdir +alias org.eolang.txt.sprintf +alias org.eolang.txt.text -+alias org.eolang.sys.os +architect yegor256@gmail.com +home https://github.com/objectionary/eo +tests @@ -75,27 +74,17 @@ (tmpdir.resolve "foo").tmpfile > @ # Test. -# @todo #3251:30min Enable the test on windows. The test fails on windows -# because `file.resolved` object for some reason produces invalid path -# on windows with unreadable characters. It appends "foo" to origin path -# and in "your-path\foo\bar" '\f' is understood as <0x00c> (form freed) unreadable -# character, '\b' is understood as <0x08> (backspace) unreadable character. -# So the result path is invalid and does not work. Maybe problem is related to -# `unescapedJavaString` function in `org.eolang.Data.ToPhi` object. But it seems it -# works fine. [] > resolves-and-touches dir (tmpdir.resolved "foo/bar") > resolved resolved.tmpfile > f - or. > @ - os.is-windows - seq - * - resolved.deleted.made.path - and. - f.exists - contains. - text f.path - "bar" + seq > @ + * + resolved.deleted.made.path + and. + f.exists + contains. + text f.path + "bar" # Test. [] > moves-a-file