2003-06-15 15:41:32 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2003-06-16 15:33:38 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2003-06-15 15:41:32 +02:00
|
|
|
#include "hash.hh"
|
2003-06-20 12:40:25 +02:00
|
|
|
#include "archive.hh"
|
2003-06-16 15:33:38 +02:00
|
|
|
#include "util.hh"
|
2003-07-20 21:29:38 +02:00
|
|
|
#include "normalise.hh"
|
2003-06-16 15:33:38 +02:00
|
|
|
#include "globals.hh"
|
2003-06-15 15:41:32 +02:00
|
|
|
|
2003-06-16 15:33:38 +02:00
|
|
|
|
2003-07-15 18:28:54 +02:00
|
|
|
void realise(FSId id)
|
2003-06-15 15:41:32 +02:00
|
|
|
{
|
2003-07-24 10:53:43 +02:00
|
|
|
Nest nest(lvlDebug, format("TEST: realising %1%") % (string) id);
|
2003-07-15 18:28:54 +02:00
|
|
|
Slice slice = normaliseFState(id);
|
|
|
|
realiseSlice(slice);
|
2003-06-16 15:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 18:28:54 +02:00
|
|
|
#if 0
|
2003-06-27 15:55:12 +02:00
|
|
|
void realiseFail(FState fs)
|
2003-06-27 11:55:31 +02:00
|
|
|
{
|
|
|
|
try {
|
2003-06-27 15:55:12 +02:00
|
|
|
realiseFState(fs);
|
2003-06-27 11:55:31 +02:00
|
|
|
abort();
|
|
|
|
} catch (Error e) {
|
|
|
|
cout << "error (expected): " << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
2003-07-15 18:28:54 +02:00
|
|
|
#endif
|
2003-06-27 11:55:31 +02:00
|
|
|
|
|
|
|
|
2003-06-16 17:59:23 +02:00
|
|
|
struct MySink : DumpSink
|
|
|
|
{
|
|
|
|
virtual void operator () (const unsigned char * data, unsigned int len)
|
|
|
|
{
|
2003-06-18 16:34:43 +02:00
|
|
|
/* Don't use cout, it's slow as hell! */
|
2003-07-20 23:11:43 +02:00
|
|
|
writeFull(STDOUT_FILENO, data, len);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct MySource : RestoreSource
|
|
|
|
{
|
2003-07-20 23:11:43 +02:00
|
|
|
virtual void operator () (unsigned char * data, unsigned int len)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
2003-07-20 23:11:43 +02:00
|
|
|
readFull(STDIN_FILENO, data, len);
|
2003-06-16 17:59:23 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-06-16 15:33:38 +02:00
|
|
|
void runTests()
|
|
|
|
{
|
|
|
|
/* Hashing. */
|
|
|
|
string s = "0b0ffd0538622bfe20b92c4aa57254d9";
|
|
|
|
Hash h = parseHash(s);
|
|
|
|
if ((string) h != s) abort();
|
|
|
|
|
|
|
|
try {
|
|
|
|
h = parseHash("blah blah");
|
|
|
|
abort();
|
|
|
|
} catch (BadRefError err) { };
|
|
|
|
|
|
|
|
try {
|
|
|
|
h = parseHash("0b0ffd0538622bfe20b92c4aa57254d99");
|
|
|
|
abort();
|
|
|
|
} catch (BadRefError err) { };
|
|
|
|
|
2003-07-08 21:58:41 +02:00
|
|
|
/* Path canonicalisation. */
|
|
|
|
cout << canonPath("/./../././//") << endl;
|
|
|
|
cout << canonPath("/foo/bar") << endl;
|
|
|
|
cout << canonPath("///foo/////bar//") << endl;
|
|
|
|
cout << canonPath("/././/foo/////bar//.") << endl;
|
|
|
|
cout << canonPath("/foo////bar//..///x/") << endl;
|
|
|
|
cout << canonPath("/foo////bar//..//..//x/y/../z/") << endl;
|
|
|
|
cout << canonPath("/foo/bar/../../../..///") << endl;
|
|
|
|
|
2003-06-16 17:59:23 +02:00
|
|
|
/* Dumping. */
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
MySink sink;
|
|
|
|
dumpPath("scratch", sink);
|
|
|
|
cout << (string) hashPath("scratch") << endl;
|
|
|
|
#endif
|
2003-06-16 15:33:38 +02:00
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
/* Restoring. */
|
2003-06-23 16:40:49 +02:00
|
|
|
#if 0
|
2003-06-23 15:27:59 +02:00
|
|
|
MySource source;
|
|
|
|
restorePath("outdir", source);
|
|
|
|
cout << (string) hashPath("outdir") << endl;
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2003-06-16 15:33:38 +02:00
|
|
|
/* Set up the test environment. */
|
|
|
|
|
|
|
|
mkdir("scratch", 0777);
|
2003-06-15 15:41:32 +02:00
|
|
|
|
2003-06-16 15:33:38 +02:00
|
|
|
string testDir = absPath("scratch");
|
|
|
|
cout << testDir << endl;
|
|
|
|
|
2003-06-27 15:55:12 +02:00
|
|
|
nixStore = testDir;
|
2003-06-16 15:33:38 +02:00
|
|
|
nixLogDir = testDir;
|
|
|
|
nixDB = testDir + "/db";
|
|
|
|
|
|
|
|
initDB();
|
|
|
|
|
|
|
|
/* Expression evaluation. */
|
|
|
|
|
2003-07-15 18:28:54 +02:00
|
|
|
FSId builder1id;
|
2003-06-27 15:55:12 +02:00
|
|
|
string builder1fn;
|
2003-07-15 18:28:54 +02:00
|
|
|
addToStore("./test-builder-1.sh", builder1fn, builder1id);
|
2003-06-27 15:55:12 +02:00
|
|
|
|
2003-07-20 21:29:38 +02:00
|
|
|
ATerm fs1 = ATmake(
|
2003-07-15 18:28:54 +02:00
|
|
|
"Slice([<str>], [(<str>, <str>, [])])",
|
|
|
|
((string) builder1id).c_str(),
|
2003-06-27 15:55:12 +02:00
|
|
|
builder1fn.c_str(),
|
2003-07-15 18:28:54 +02:00
|
|
|
((string) builder1id).c_str());
|
2003-07-21 10:55:49 +02:00
|
|
|
FSId fs1id = writeTerm(fs1, "");
|
2003-07-15 18:28:54 +02:00
|
|
|
|
|
|
|
realise(fs1id);
|
|
|
|
realise(fs1id);
|
|
|
|
|
2003-07-20 21:29:38 +02:00
|
|
|
ATerm fs2 = ATmake(
|
2003-07-15 18:28:54 +02:00
|
|
|
"Slice([<str>], [(<str>, <str>, [])])",
|
|
|
|
((string) builder1id).c_str(),
|
|
|
|
(builder1fn + "_bla").c_str(),
|
|
|
|
((string) builder1id).c_str());
|
2003-07-21 10:55:49 +02:00
|
|
|
FSId fs2id = writeTerm(fs2, "");
|
2003-07-15 18:28:54 +02:00
|
|
|
|
|
|
|
realise(fs2id);
|
|
|
|
realise(fs2id);
|
|
|
|
|
|
|
|
string out1id = hashString("foo"); /* !!! bad */
|
2003-07-15 23:24:05 +02:00
|
|
|
string out1fn = nixStore + "/" + (string) out1id + "-hello.txt";
|
2003-07-20 21:29:38 +02:00
|
|
|
ATerm fs3 = ATmake(
|
2003-07-15 18:28:54 +02:00
|
|
|
"Derive([(<str>, <str>)], [<str>], <str>, <str>, [(\"out\", <str>)])",
|
|
|
|
out1fn.c_str(),
|
|
|
|
((string) out1id).c_str(),
|
|
|
|
((string) fs1id).c_str(),
|
|
|
|
((string) builder1fn).c_str(),
|
|
|
|
thisSystem.c_str(),
|
|
|
|
out1fn.c_str());
|
|
|
|
debug(printTerm(fs3));
|
2003-07-21 10:55:49 +02:00
|
|
|
FSId fs3id = writeTerm(fs3, "");
|
2003-07-15 18:28:54 +02:00
|
|
|
|
|
|
|
realise(fs3id);
|
|
|
|
realise(fs3id);
|
2003-06-16 15:33:38 +02:00
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
|
|
|
|
FSId builder4id;
|
|
|
|
string builder4fn;
|
|
|
|
addToStore("./test-builder-2.sh", builder4fn, builder4id);
|
|
|
|
|
2003-07-20 21:29:38 +02:00
|
|
|
ATerm fs4 = ATmake(
|
2003-07-15 23:24:05 +02:00
|
|
|
"Slice([<str>], [(<str>, <str>, [])])",
|
|
|
|
((string) builder4id).c_str(),
|
|
|
|
builder4fn.c_str(),
|
|
|
|
((string) builder4id).c_str());
|
2003-07-21 10:55:49 +02:00
|
|
|
FSId fs4id = writeTerm(fs4, "");
|
2003-07-15 23:24:05 +02:00
|
|
|
|
|
|
|
realise(fs4id);
|
|
|
|
|
|
|
|
string out5id = hashString("bar"); /* !!! bad */
|
|
|
|
string out5fn = nixStore + "/" + (string) out5id + "-hello2";
|
2003-07-20 21:29:38 +02:00
|
|
|
ATerm fs5 = ATmake(
|
2003-07-15 23:24:05 +02:00
|
|
|
"Derive([(<str>, <str>)], [<str>], <str>, <str>, [(\"out\", <str>), (\"builder\", <str>)])",
|
|
|
|
out5fn.c_str(),
|
|
|
|
((string) out5id).c_str(),
|
|
|
|
((string) fs4id).c_str(),
|
|
|
|
((string) builder4fn).c_str(),
|
|
|
|
thisSystem.c_str(),
|
|
|
|
out5fn.c_str(),
|
|
|
|
((string) builder4fn).c_str());
|
|
|
|
debug(printTerm(fs5));
|
2003-07-21 10:55:49 +02:00
|
|
|
FSId fs5id = writeTerm(fs5, "");
|
2003-07-15 23:24:05 +02:00
|
|
|
|
|
|
|
realise(fs5id);
|
|
|
|
realise(fs5id);
|
2003-06-16 15:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-07 11:25:26 +02:00
|
|
|
void run(Strings args)
|
2003-06-16 15:33:38 +02:00
|
|
|
{
|
2003-07-07 11:25:26 +02:00
|
|
|
runTests();
|
2003-06-15 15:41:32 +02:00
|
|
|
}
|
2003-07-07 11:25:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
string programId = "test";
|