Skip to content

Commit

Permalink
🔀 Merge branch 'mmyster/feature/test-functional-file-manager' into de…
Browse files Browse the repository at this point in the history
…velop
  • Loading branch information
ladislas committed Oct 31, 2022
2 parents ad37a1c + ec627b2 commit 25050e2
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 7 deletions.
1 change: 1 addition & 0 deletions tests/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ endfunction()
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/boost_ut)

add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/io_expander)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/file_manager)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/qdac)
40 changes: 33 additions & 7 deletions tests/functional/include/tests/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,44 @@

#include "rtos/ThisThread.h"

#include "FATFileSystem.h"
#include "LogKit.h"
#include "SDBlockDevice.h"

namespace utils::time {
namespace utils {

using namespace leka;
using namespace std::chrono;
namespace time {

inline auto start = rtos::Kernel::Clock::now();
inline auto stop = rtos::Kernel::Clock::now();
inline auto delta = [] { return static_cast<int>((stop - start).count()); };
using namespace leka;
using namespace std::chrono;

} // namespace utils::time
inline auto start = rtos::Kernel::Clock::now();
inline auto stop = rtos::Kernel::Clock::now();
inline auto delta = [] { return static_cast<int>((stop - start).count()); };

} // namespace time

namespace sd {

namespace internal {

inline auto bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK};
inline auto fs = FATFileSystem {"fs"};

constexpr auto default_frequency = uint64_t {25'000'000};

} // namespace internal

inline void init()
{
internal::bd.init();
internal::bd.frequency(internal::default_frequency);
internal::fs.mount(&internal::bd);
}

} // namespace sd

} // namespace utils

// NOLINTNEXTLINE
#define utils_start(msg) \
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/tests/file_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Leka - LekaOS
# Copyright 2022 APF France handicap
# SPDX-License-Identifier: Apache-2.0

register_functional_test(
TARGET
functional_ut_file_manager

INCLUDE_DIRECTORIES

SOURCES
suite_file_manager.cpp

LINK_LIBRARIES
FileManagerKit
)
111 changes: 111 additions & 0 deletions tests/functional/tests/file_manager/suite_file_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include <cstddef>

#include "FileManagerKit.h"
#include "boost/ut.hpp"
#include "filesystem"
#include "tests/config.h"
#include "tests/utils.h"

using namespace leka;
using namespace boost::ut;

struct path {
static inline const auto root = std::filesystem::path {"/fs/var/tmp/test"};

static inline const auto dir = root / "dir" / "";
static inline const auto sub_dir = root / "dir" / "sub";
static inline const auto dir_file = root / "dir" / "dir_file.txt";
static inline const auto sub_dir_file = root / "dir" / "sub" / "sub_dir_file.txt";

static inline const auto all = std::to_array({dir_file, sub_dir, dir, root});

static inline auto remove_all = []() {
log << "Cleaning up files, directories";

for (const auto &p: path::all) {
if (std::filesystem::exists(p)) {
log << "Removing:" << p;
std::filesystem::remove(p);
expect(not std::filesystem::exists(p)) << p << "still exists";
} else {
log << "Doesn't exit:" << p;
}
}
log << "\n";
};
};

suite suite_file_manager_kit = [] {
auto file = FileManagerKit::File {};
auto input_data = std::to_array("Hello, Leka!");

utils::sd::init();
path::remove_all();

"open existing file"_test = [&] {
auto open = file.open("/fs/usr/test/file-1-ok.txt", "w");

expect(open >> fatal) << "Failed to open file";
};

"create root directory"_test = [&] {
auto created = FileManagerKit::create_directory(path::root);

expect(created >> fatal) << "Failed to create directory" << path::root;
};

"create directory"_test = [&] {
auto created = FileManagerKit::create_directory(path::dir);

expect(created >> fatal) << "Failed to create directory" << path::dir;
};

"create subdirectory"_test = [&] {
auto created = FileManagerKit::create_directory(path::sub_dir);

expect(created >> fatal) << "Failed to create directory";
};

"create & open new file"_test = [&] {
auto open = file.open(path::dir_file, "w");
auto exists = std::filesystem::exists(path::dir_file);

expect(open >> fatal) << "Failed to open file";
expect(exists >> fatal) << "Failed to create file";
};

"close new file"_test = [&] {
file.close();

expect(not file.is_open()) << "Failed to close file";
};

"write to new file "_test = [&] {
file.open(path::dir_file, "w");
auto bytes = file.write(input_data);
file.close();

expect(bytes == std::size(input_data) >> fatal) << "Failed to write file";
};

"read new file "_test = [&] {
auto output_data = std::array<char, 64> {};

file.open(path::dir_file, "r");
auto bytes = file.read(output_data);
file.close();

expect(bytes == std::size(input_data) >> fatal) << "Failed to read file, bytes =" << bytes;

auto found = std::ranges::search(output_data, input_data);

expect(not found.empty());
};

file.close();
path::remove_all();
};

0 comments on commit 25050e2

Please sign in to comment.