Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #3

Merged
merged 2 commits into from
Nov 10, 2024
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)

project(
structarus
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Structarus

Creates structure of directories and files inside tar archive.
Creates structure of directories and files inside tar archive. This library doesn't compress anything. It is just take files with their containing and produces tape archive with them inside it.

Structarus can create archive in memory and on the disk.

## How to use
See in tests.
2 changes: 1 addition & 1 deletion include/structarus/detail/tar_to_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void tar_to_stream(T & stream, /// stream to write to, e.g. ostream
auto const strFilemode = detail::getStringFilemode(info.filemode);

detail::Header header;
detail::toArrayFromString(info.archiveName, header.name);
detail::toArrayFromString(info.filename, header.name);
detail::toArrayFromString(strFilemode, header.mode);
detail::toArrayFromString(info.uname, header.uname);
detail::toArrayFromString(info.gname, header.gname);
Expand Down
4 changes: 2 additions & 2 deletions include/structarus/tar_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ enum class FileType {
};

struct FileInfo {
std::string const & archiveName; /// name of the file to write in tar
std::string const & data; /// data for writing
std::string const & filename; /// name of the file to write in tar
std::string const & data; /// data for writing
FileType fileType = FileType::RegularFile;
Filemode filemode = Filemode::ReadWriteExecute;
uint64_t mtime = 0; /// file modification time, in seconds since epoch
Expand Down
71 changes: 50 additions & 21 deletions tests/tar/tar_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
#include <filesystem>
#include <gtest/gtest.h>
#include <sstream>

#include "include/structarus/tar_creator.hpp"
#include "include/structarus/tar_info.hpp"

using namespace tar;
namespace fs = std::filesystem;

TEST(TarHeader, BasicConstruction) {
detail::Header header;
EXPECT_EQ(header.name.size(), 100u);
EXPECT_EQ(header.mode.size(), 8u);
EXPECT_EQ(header.uid.size(), 8u);
EXPECT_EQ(header.gid.size(), 8u);
EXPECT_EQ(header.size.size(), 12u);
EXPECT_EQ(header.mtime.size(), 12u);
EXPECT_EQ(header.checksum.size(), 8u);
EXPECT_EQ(header.typeflag, '0');
EXPECT_EQ(header.linkname.size(), 100u);
EXPECT_EQ(header.magic.size(), 6u);
EXPECT_EQ(header.version.size(), 2u);
EXPECT_EQ(header.uname.size(), 32u);
EXPECT_EQ(header.gname.size(), 32u);
EXPECT_EQ(header.devmajor.size(), 8u);
EXPECT_EQ(header.devminor.size(), 8u);
EXPECT_EQ(header.prefix.size(), 155u);
EXPECT_EQ(header.padding.size(), 12u); // Adjusted size to match the actual padding size
detail::Header header;
EXPECT_EQ(header.name.size(), 100u);
EXPECT_EQ(header.mode.size(), 8u);
EXPECT_EQ(header.uid.size(), 8u);
EXPECT_EQ(header.gid.size(), 8u);
EXPECT_EQ(header.size.size(), 12u);
EXPECT_EQ(header.mtime.size(), 12u);
EXPECT_EQ(header.checksum.size(), 8u);
EXPECT_EQ(header.typeflag, '0');
EXPECT_EQ(header.linkname.size(), 100u);
EXPECT_EQ(header.magic.size(), 6u);
EXPECT_EQ(header.version.size(), 2u);
EXPECT_EQ(header.uname.size(), 32u);
EXPECT_EQ(header.gname.size(), 32u);
EXPECT_EQ(header.devmajor.size(), 8u);
EXPECT_EQ(header.devminor.size(), 8u);
EXPECT_EQ(header.prefix.size(), 155u);
EXPECT_EQ(header.padding.size(),
12u); // Adjusted size to match the actual padding size
}

TEST(Tar, Dummy) {
std::string result;
tar::Creator<std::ofstream> creator(result);
creator.addFile({"sqwoz", "bab"});
std::string filename{"altushka.tar"};
{
// Note: creator destructor is needed
tar::Creator<std::ofstream> creator(filename);
creator.addFile({"sqwoz.txt", "bab"});
}
fs::remove(filename);
}

TEST(Tar, Example) {

// filename must contain absolute path indise tar
// e.g. my/awesome/file.txt
using FilenameAndData = std::pair<std::string, std::string>;
std::vector<FilenameAndData> files{{"first/file.cpp", "#include <iostream>"},
{"second/file.py", "import os"}};

std::string inMemoryTar;
{
// Note: creator destructor is needed
tar::Creator<std::ostringstream> creator(inMemoryTar);
for (auto const &file : files) {
creator.addFile({file.first, file.second, tar::FileType::RegularFile});
}
}

ASSERT_FALSE(inMemoryTar.empty());
}