Skip to content

Commit

Permalink
Merge remote-tracking branch 'obsidiansystems/ipfs-develop' into ipfs…
Browse files Browse the repository at this point in the history
…-git-ca
  • Loading branch information
matthewbauer committed Jun 23, 2020
2 parents bd871a5 + 5b22a90 commit c0314c5
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 42 deletions.
1 change: 1 addition & 0 deletions release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ let
pkgconfig
pkgs.perl
boost
nlohmann_json
]
++ lib.optional (stdenv.isLinux || stdenv.isDarwin) libsodium;

Expand Down
4 changes: 2 additions & 2 deletions src/libstore/filetransfer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ struct curlFileTransfer : public FileTransfer

std::shared_ptr<std::string> response;
if (decompressionSink)
if (auto teeSink = std::dynamic_pointer_cast<TeeSink<std::shared_ptr<CompressionSink>>>(decompressionSink))
if (auto teeSink = std::dynamic_pointer_cast<TeeSink<ref<CompressionSink>>>(decompressionSink))
response = teeSink->data;
auto exc =
code == CURLE_ABORTED_BY_CALLBACK && _isInterrupted
Expand Down Expand Up @@ -877,7 +877,7 @@ FileTransferError::FileTransferError(FileTransfer::Error error, std::shared_ptr<
{
const auto hf = hintfmt(args...);
if (response) {
err.hint = hintfmt("%1%\n\nresponse body:\n\n%2%", normaltxt(hf.str()), response);
err.hint = hintfmt("%1%\n\nresponse body:\n\n%2%", normaltxt(hf.str()), *response);
} else {
err.hint = hf;
}
Expand Down
11 changes: 9 additions & 2 deletions src/libstore/ipfs-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ class IPFSBinaryCacheStore : public Store
}

public:
Path formatPathAsProtocol(Path path) {
if (hasPrefix(path, "/ipfs/"))
return "ipfs://" + path.substr(strlen("/ipfs/"), string::npos);
else if (hasPrefix(path, "/ipns/"))
return "ipns://" + path.substr(strlen("/ipfs/"), string::npos);
else return path;
}

// IPNS publish can be slow, we try to do it rarely.
void sync() override
Expand All @@ -232,8 +239,8 @@ class IPFSBinaryCacheStore : public Store
return;

if (!optIpnsPath) {
throw Error("We don't have an ipns path and the current ipfs address doesn't match the initial one.\n current: %s\n initial: %s",
state->ipfsPath, initialIpfsPath);
throw Error("The current IPFS address doesn't match the configured one. \n initial: %s\n current: %s",
formatPathAsProtocol(initialIpfsPath), formatPathAsProtocol(state->ipfsPath));
}

auto ipnsPath = *optIpnsPath;
Expand Down
7 changes: 5 additions & 2 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ StoreType getStoreType(const std::string & uri, const std::string & stateDir)
{
if (uri == "daemon") {
return tDaemon;
} else if (uri == "local" || hasPrefix(uri, "/")) {
} else if (uri == "local" || hasPrefix(uri, "/") || hasPrefix(uri, "./")) {
return tLocal;
} else if (uri == "" || uri == "auto") {
if (access(stateDir.c_str(), R_OK | W_OK) == 0)
Expand All @@ -948,8 +948,11 @@ static RegisterStoreImplementation regStore([](
return std::shared_ptr<Store>(std::make_shared<UDSRemoteStore>(params));
case tLocal: {
Store::Params params2 = params;
if (hasPrefix(uri, "/"))
if (hasPrefix(uri, "/")) {
params2["root"] = uri;
} else if (hasPrefix(uri, "./")) {
params2["root"] = absPath(uri);
}
return std::shared_ptr<Store>(std::make_shared<LocalStore>(params2));
}
default:
Expand Down
17 changes: 16 additions & 1 deletion src/libutil/fmt.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <boost/format.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <string>
#include "ansicolor.hh"

Expand Down Expand Up @@ -103,7 +104,9 @@ class hintformat
public:
hintformat(const string &format) :fmt(format)
{
fmt.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
fmt.exceptions(boost::io::all_error_bits ^
boost::io::too_many_args_bit ^
boost::io::too_few_args_bit);
}

hintformat(const hintformat &hf)
Expand All @@ -117,6 +120,13 @@ public:
return *this;
}

template<class T>
hintformat& operator%(const normaltxt<T> &value)
{
fmt % value.value;
return *this;
}

std::string str() const
{
return fmt.str();
Expand All @@ -136,4 +146,9 @@ inline hintformat hintfmt(const std::string & fs, const Args & ... args)
return f;
}

inline hintformat hintfmt(std::string plain_string)
{
// we won't be receiving any args in this case, so just print the original string
return hintfmt("%s", normaltxt(plain_string));
}
}
99 changes: 68 additions & 31 deletions src/libutil/tests/logging.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "logging.hh"
#include "nixexpr.hh"
#include "util.hh"
#include <fstream>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -42,7 +43,7 @@ namespace nix {
logger->logEI(ei);
auto str = testing::internal::GetCapturedStderr();

ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- TestError --- error-unit-test\x1B[0m\n\x1B[33;1m\x1B[0minitial error\x1B[0m; subsequent error message.\n");
ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- TestError --- error-unit-test\x1B[0m\ninitial error; subsequent error message.\n");
}

}
Expand All @@ -60,18 +61,17 @@ namespace nix {
logError(e.info());
auto str = testing::internal::GetCapturedStderr();

ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- SysError --- error-unit-test\x1B[0m\n\x1B[33;1m\x1B[0mstatting file\x1B[0m: \x1B[33;1mBad file descriptor\x1B[0m\n");

ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- SysError --- error-unit-test\x1B[0m\nstatting file: \x1B[33;1mBad file descriptor\x1B[0m\n");
}
}

TEST(logEI, loggingErrorOnInfoLevel) {
testing::internal::CaptureStderr();

logger->logEI({ .level = lvlInfo,
.name = "Info name",
.description = "Info description",
});
.name = "Info name",
.description = "Info description",
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[32;1minfo:\x1B[0m\x1B[34;1m --- Info name --- error-unit-test\x1B[0m\nInfo description\n");
Expand All @@ -85,7 +85,7 @@ namespace nix {
logger->logEI({ .level = lvlTalkative,
.name = "Talkative name",
.description = "Talkative description",
});
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[32;1mtalk:\x1B[0m\x1B[34;1m --- Talkative name --- error-unit-test\x1B[0m\nTalkative description\n");
Expand All @@ -99,7 +99,7 @@ namespace nix {
logger->logEI({ .level = lvlChatty,
.name = "Chatty name",
.description = "Talkative description",
});
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[32;1mchat:\x1B[0m\x1B[34;1m --- Chatty name --- error-unit-test\x1B[0m\nTalkative description\n");
Expand All @@ -113,7 +113,7 @@ namespace nix {
logger->logEI({ .level = lvlDebug,
.name = "Debug name",
.description = "Debug description",
});
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[33;1mdebug:\x1B[0m\x1B[34;1m --- Debug name --- error-unit-test\x1B[0m\nDebug description\n");
Expand All @@ -127,7 +127,7 @@ namespace nix {
logger->logEI({ .level = lvlVomit,
.name = "Vomit name",
.description = "Vomit description",
});
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[32;1mvomit:\x1B[0m\x1B[34;1m --- Vomit name --- error-unit-test\x1B[0m\nVomit description\n");
Expand All @@ -144,7 +144,7 @@ namespace nix {
logError({
.name = "name",
.description = "error description",
});
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- name --- error-unit-test\x1B[0m\nerror description\n");
Expand All @@ -160,13 +160,13 @@ namespace nix {
.name = "error name",
.description = "error with code lines",
.hint = hintfmt("this hint has %1% templated %2%!!",
"yellow",
"values"),
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = "previous line of code",
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = "next line of code",
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = "previous line of code",
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = "next line of code",
}});


Expand All @@ -183,10 +183,10 @@ namespace nix {
.name = "error name",
.description = "error without any code lines.",
.hint = hintfmt("this hint has %1% templated %2%!!",
"yellow",
"values"),
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13)
.errPos = Pos(problem_file, 40, 13)
}});

auto str = testing::internal::GetCapturedStderr();
Expand All @@ -202,7 +202,7 @@ namespace nix {
.name = "error name",
.hint = hintfmt("hint %1%", "only"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13)
.errPos = Pos(problem_file, 40, 13)
}});

auto str = testing::internal::GetCapturedStderr();
Expand All @@ -218,10 +218,10 @@ namespace nix {
testing::internal::CaptureStderr();

logWarning({
.name = "name",
.description = "error description",
.hint = hintfmt("there was a %1%", "warning"),
});
.name = "name",
.description = "error description",
.hint = hintfmt("there was a %1%", "warning"),
});

auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[33;1mwarning:\x1B[0m\x1B[34;1m --- name --- error-unit-test\x1B[0m\nerror description\n\nthere was a \x1B[33;1mwarning\x1B[0m\n");
Expand All @@ -238,18 +238,55 @@ namespace nix {
.name = "warning name",
.description = "warning description",
.hint = hintfmt("this hint has %1% templated %2%!!",
"yellow",
"values"),
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = std::nullopt,
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::nullopt
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = std::nullopt,
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::nullopt
}});


auto str = testing::internal::GetCapturedStderr();
ASSERT_STREQ(str.c_str(), "\x1B[33;1mwarning:\x1B[0m\x1B[34;1m --- warning name --- error-unit-test\x1B[0m\nin file: \x1B[34;1mmyfile.nix (40:13)\x1B[0m\n\nwarning description\n\n 40| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n\nthis hint has \x1B[33;1myellow\x1B[0m templated \x1B[33;1mvalues\x1B[0m!!\n");
}

/* ----------------------------------------------------------------------------
* hintfmt
* --------------------------------------------------------------------------*/

TEST(hintfmt, percentStringWithoutArgs) {

const char *teststr = "this is 100%s correct!";

ASSERT_STREQ(
hintfmt(teststr).str().c_str(),
teststr);

}

TEST(hintfmt, fmtToHintfmt) {

ASSERT_STREQ(
hintfmt(fmt("the color of this this text is %1%", "not yellow")).str().c_str(),
"the color of this this text is not yellow");

}

TEST(hintfmt, tooFewArguments) {

ASSERT_STREQ(
hintfmt("only one arg %1% %2%", "fulfilled").str().c_str(),
"only one arg " ANSI_YELLOW "fulfilled" ANSI_NORMAL " ");

}

TEST(hintfmt, tooManyArguments) {

ASSERT_STREQ(
hintfmt("what about this %1% %2%", "%3%", "one", "two").str().c_str(),
"what about this " ANSI_YELLOW "%3%" ANSI_NORMAL " " ANSI_YELLOW "one" ANSI_NORMAL);

}
}
8 changes: 4 additions & 4 deletions tests/ipfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ nix-build ./fixed.nix -A good \
EMPTY_HASH=$(echo {} | ipfs dag put)

# Try to upload the content to the empty directory, fail but grab the right hash
IPFS_HASH=$(set -e; \
IPFS_ADDRESS=$(set -e; \
set -o pipefail; \
! nix copy --to ipfs://$EMPTY_HASH $(nix-build ./fixed.nix -A good) --experimental-features nix-command \
|& grep current: | awk '{print substr($2, 7, length($2))}')
|& grep current: | awk '{print $2}')

# Verify that new path is valid.
nix copy --to ipfs://$IPFS_HASH $(nix-build ./fixed.nix -A good) --experimental-features nix-command
nix copy --to $IPFS_ADDRESS $(nix-build ./fixed.nix -A good) --experimental-features nix-command

mkdir $IPFS_DST_IPFS_STORE

nix-build ./fixed.nix -A good \
--option substituters 'ipfs://'$IPFS_HASH \
--option substituters $IPFS_ADDRESS \
--store $IPFS_DST_IPFS_STORE \
--no-out-link \
-j0 \
Expand Down

0 comments on commit c0314c5

Please sign in to comment.