Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/yup_standalone.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function (yup_standalone_app)
-sSTACK_OVERFLOW_CHECK=2
-sFORCE_FILESYSTEM=1
-sNODERAWFS=0
-sWASMFS=1
-sFETCH=1
-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='$dynCall'
--shell-file "${YUP_ARG_CUSTOM_SHELL}")
Expand Down
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ emscripten CONFIG="Debug":
emcmake cmake -G "Ninja Multi-Config" -B build
@just build {{CONFIG}}

[doc("run tests for WASM")]
emscripten_test CONFIG="Debug":
emcmake cmake -G "Ninja Multi-Config" -B build
@just build {{CONFIG}}
node build/tests/{{CONFIG}}/yup_tests.js --gtest_filter=*

[doc("serve project for WASM")]
emscripten_serve CONFIG="Debug":
python3 -m http.server -d .
Expand Down
254 changes: 1 addition & 253 deletions modules/yup_core/files/yup_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ bool File::createSymbolicLink (const File& linkFileToCreate,
linkFileToCreate.deleteFile();
}

#if YUP_MAC || YUP_LINUX || YUP_BSD
#if YUP_MAC || YUP_LINUX || YUP_BSD || YUP_WASM
// one common reason for getting an error here is that the file already exists
if (symlink (nativePathOfTarget.toRawUTF8(), linkFileToCreate.getFullPathName().toRawUTF8()) == -1)
{
Expand Down Expand Up @@ -1099,256 +1099,4 @@ MemoryMappedFile::MemoryMappedFile (const File& file, const Range<int64>& fileRa
openInternal (file, mode, exclusive);
}

//==============================================================================
//==============================================================================
#if YUP_UNIT_TESTS

class FileTests final : public UnitTest
{
public:
FileTests()
: UnitTest ("Files", UnitTestCategories::files)
{
}

void runTest() override
{
beginTest ("Reading");

const File home (File::getSpecialLocation (File::userHomeDirectory));
const File temp (File::getSpecialLocation (File::tempDirectory));

expect (! File().exists());
expect (! File().existsAsFile());
expect (! File().isDirectory());
#if ! YUP_WINDOWS
expect (File ("/").isDirectory());
#endif
expect (home.isDirectory());
expect (home.exists());
expect (! home.existsAsFile());
expect (File::getSpecialLocation (File::userApplicationDataDirectory).isDirectory());
expect (File::getSpecialLocation (File::currentExecutableFile).exists());
expect (File::getSpecialLocation (File::currentApplicationFile).exists());
expect (File::getSpecialLocation (File::invokedExecutableFile).exists());
expect (home.getVolumeTotalSize() > 1024 * 1024);
expect (home.getBytesFreeOnVolume() > 0);
expect (! home.isHidden());
expect (home.isOnHardDisk());
expect (! home.isOnCDRomDrive());
expect (File::getCurrentWorkingDirectory().exists());
expect (home.setAsCurrentWorkingDirectory());

{
auto homeParent = home;
bool noSymlinks = true;

while (! homeParent.isRoot())
{
if (homeParent.isSymbolicLink())
{
noSymlinks = false;
break;
}

homeParent = homeParent.getParentDirectory();
}

if (noSymlinks)
expect (File::getCurrentWorkingDirectory() == home);
}

{
Array<File> roots;
File::findFileSystemRoots (roots);
expect (roots.size() > 0);

int numRootsExisting = 0;
for (int i = 0; i < roots.size(); ++i)
if (roots[i].exists())
++numRootsExisting;

// (on windows, some of the drives may not contain media, so as long as at least one is ok..)
expect (numRootsExisting > 0);
}

beginTest ("Writing");

auto random = getRandom();
const auto tempFolderName = "YUP UnitTests Temp Folder "
+ String::toHexString (random.nextInt())
+ ".folder";
File demoFolder (temp.getChildFile (tempFolderName));
expect (demoFolder.deleteRecursively());
expect (demoFolder.createDirectory());
expect (demoFolder.isDirectory());
expect (demoFolder.getParentDirectory() == temp);
expect (temp.isDirectory());
expect (temp.findChildFiles (File::findFilesAndDirectories, false, "*").contains (demoFolder));
expect (temp.findChildFiles (File::findDirectories, true, "*.folder").contains (demoFolder));

File tempFile (demoFolder.getNonexistentChildFile ("test", ".txt", false));

expect (tempFile.getFileExtension() == ".txt");
expect (tempFile.hasFileExtension (".txt"));
expect (tempFile.hasFileExtension ("txt"));
expect (tempFile.withFileExtension ("xyz").hasFileExtension (".xyz"));
expect (tempFile.withFileExtension ("xyz").hasFileExtension ("abc;xyz;foo"));
expect (tempFile.withFileExtension ("xyz").hasFileExtension ("xyz;foo"));
expect (! tempFile.withFileExtension ("h").hasFileExtension ("bar;foo;xx"));
expect (tempFile.getSiblingFile ("foo").isAChildOf (temp));
expect (tempFile.hasWriteAccess());

expect (home.getChildFile (".") == home);
expect (home.getChildFile ("..") == home.getParentDirectory());
expect (home.getChildFile (".xyz").getFileName() == ".xyz");
expect (home.getChildFile ("..xyz").getFileName() == "..xyz");
expect (home.getChildFile ("...xyz").getFileName() == "...xyz");
expect (home.getChildFile ("./xyz") == home.getChildFile ("xyz"));
expect (home.getChildFile ("././xyz") == home.getChildFile ("xyz"));
expect (home.getChildFile ("../xyz") == home.getParentDirectory().getChildFile ("xyz"));
expect (home.getChildFile (".././xyz") == home.getParentDirectory().getChildFile ("xyz"));
expect (home.getChildFile (".././xyz/./abc") == home.getParentDirectory().getChildFile ("xyz/abc"));
expect (home.getChildFile ("./../xyz") == home.getParentDirectory().getChildFile ("xyz"));
expect (home.getChildFile ("a1/a2/a3/./../../a4") == home.getChildFile ("a1/a4"));

expect (! File().hasReadAccess());
expect (! File().hasWriteAccess());

expect (! tempFile.hasReadAccess());

{
FileOutputStream fo (tempFile);
fo.write ("0123456789", 10);
}

expect (tempFile.hasReadAccess());

expect (tempFile.exists());
expect (tempFile.getSize() == 10);
expect (std::abs ((int) (tempFile.getLastModificationTime().toMilliseconds() - Time::getCurrentTime().toMilliseconds())) < 3000);
expectEquals (tempFile.loadFileAsString(), String ("0123456789"));
expect (! demoFolder.containsSubDirectories());

expectEquals (tempFile.getRelativePathFrom (demoFolder.getParentDirectory()), demoFolder.getFileName() + File::getSeparatorString() + tempFile.getFileName());
expectEquals (demoFolder.getParentDirectory().getRelativePathFrom (tempFile), ".." + File::getSeparatorString() + ".." + File::getSeparatorString() + demoFolder.getParentDirectory().getFileName());

expect (demoFolder.getNumberOfChildFiles (File::findFiles) == 1);
expect (demoFolder.getNumberOfChildFiles (File::findFilesAndDirectories) == 1);
expect (demoFolder.getNumberOfChildFiles (File::findDirectories) == 0);
demoFolder.getNonexistentChildFile ("tempFolder", "", false).createDirectory();
expect (demoFolder.getNumberOfChildFiles (File::findDirectories) == 1);
expect (demoFolder.getNumberOfChildFiles (File::findFilesAndDirectories) == 2);
expect (demoFolder.containsSubDirectories());

expect (tempFile.hasWriteAccess());
tempFile.setReadOnly (true);
expect (! tempFile.hasWriteAccess());
tempFile.setReadOnly (false);
expect (tempFile.hasWriteAccess());

Time t (Time::getCurrentTime());
tempFile.setLastModificationTime (t);
Time t2 = tempFile.getLastModificationTime();
expect (std::abs ((int) (t2.toMilliseconds() - t.toMilliseconds())) <= 1000);

{
MemoryBlock mb;
tempFile.loadFileAsData (mb);
expect (mb.getSize() == 10);
expect (mb[0] == '0');
}

{
expect (tempFile.getSize() == 10);
FileOutputStream fo (tempFile);
expect (fo.openedOk());

expect (fo.setPosition (7));
expect (fo.truncate().wasOk());
expect (tempFile.getSize() == 7);
fo.write ("789", 3);
fo.flush();
expect (tempFile.getSize() == 10);
}

beginTest ("Memory-mapped files");

{
MemoryMappedFile mmf (tempFile, MemoryMappedFile::readOnly);
expect (mmf.getSize() == 10);
expect (mmf.getData() != nullptr);
expect (memcmp (mmf.getData(), "0123456789", 10) == 0);
}

{
const File tempFile2 (tempFile.getNonexistentSibling (false));
expect (tempFile2.create());
expect (tempFile2.appendData ("xxxxxxxxxx", 10));

{
MemoryMappedFile mmf (tempFile2, MemoryMappedFile::readWrite);
expect (mmf.getSize() == 10);
expect (mmf.getData() != nullptr);
memcpy (mmf.getData(), "abcdefghij", 10);
}

{
MemoryMappedFile mmf (tempFile2, MemoryMappedFile::readWrite);
expect (mmf.getSize() == 10);
expect (mmf.getData() != nullptr);
expect (memcmp (mmf.getData(), "abcdefghij", 10) == 0);
}

expect (tempFile2.deleteFile());
}

beginTest ("More writing");

expect (tempFile.appendData ("abcdefghij", 10));
expect (tempFile.getSize() == 20);
expect (tempFile.replaceWithData ("abcdefghij", 10));
expect (tempFile.getSize() == 10);

File tempFile2 (tempFile.getNonexistentSibling (false));
expect (tempFile.copyFileTo (tempFile2));
expect (tempFile2.exists());
expect (tempFile2.hasIdenticalContentTo (tempFile));
expect (tempFile.deleteFile());
expect (! tempFile.exists());
expect (tempFile2.moveFileTo (tempFile));
expect (tempFile.exists());
expect (! tempFile2.exists());

expect (demoFolder.deleteRecursively());
expect (! demoFolder.exists());

{
URL url ("https://audio.dev/foo/bar/");
expectEquals (url.toString (false), String ("https://audio.dev/foo/bar/"));
expectEquals (url.getChildURL ("x").toString (false), String ("https://audio.dev/foo/bar/x"));
expectEquals (url.getParentURL().toString (false), String ("https://audio.dev/foo"));
expectEquals (url.getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
expectEquals (url.getParentURL().getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
expectEquals (url.getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/foo/x"));
expectEquals (url.getParentURL().getParentURL().getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/x"));
}

{
URL url ("https://audio.dev/foo/bar");
expectEquals (url.toString (false), String ("https://audio.dev/foo/bar"));
expectEquals (url.getChildURL ("x").toString (false), String ("https://audio.dev/foo/bar/x"));
expectEquals (url.getParentURL().toString (false), String ("https://audio.dev/foo"));
expectEquals (url.getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
expectEquals (url.getParentURL().getParentURL().getParentURL().toString (false), String ("https://audio.dev/"));
expectEquals (url.getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/foo/x"));
expectEquals (url.getParentURL().getParentURL().getParentURL().getChildURL ("x").toString (false), String ("https://audio.dev/x"));
}
}
};

static FileTests fileUnitTests;

#endif

} // namespace yup
41 changes: 9 additions & 32 deletions modules/yup_core/native/yup_Files_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
namespace yup
{

//==============================================================================
const char* const* yup_argv = nullptr;
int yup_argc = 0;

//==============================================================================
enum
{
U_ISOFS_SUPER_MAGIC = 0x9660, // linux/iso_fs.h
Expand Down Expand Up @@ -63,7 +68,7 @@ bool File::isOnHardDisk() const

bool File::isOnRemovableDrive() const
{
jassertfalse; // xxx not implemented for linux!
jassertfalse; // xxx not yet implemented for wasm!
return false;
}

Expand All @@ -72,21 +77,7 @@ String File::getVersion() const
return {}; // xxx not yet implemented
}

bool File::isSymbolicLink() const
{
return false; // xxx not yet implemented
}

String File::getNativeLinkedTarget() const
{
return {}; // xxx not yet implemented
}

//==============================================================================

const char* const* yup_argv = nullptr;
int yup_argc = 0;

File File::getSpecialLocation (const SpecialLocationType type)
{
/*
Expand Down Expand Up @@ -181,27 +172,13 @@ void File::revealToUser() const
}

//==============================================================================
class DirectoryIterator::NativeIterator::Pimpl
void MemoryMappedFile::openInternal (const File& file, AccessMode mode, bool exclusive)
{
};

DirectoryIterator::NativeIterator::NativeIterator (const File& directory, const String& wildCardStr)
{
ignoreUnused (directory, wildCardStr);
jassertfalse;
}

DirectoryIterator::NativeIterator::~NativeIterator() {}

bool DirectoryIterator::NativeIterator::next (String& filenameFound,
bool* isDir,
bool* isHidden,
int64* fileSize,
Time* modTime,
Time* creationTime,
bool* isReadOnly)
MemoryMappedFile::~MemoryMappedFile()
{
ignoreUnused (filenameFound, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);
return false;
}

//==============================================================================
Expand Down
Loading
Loading