2003-06-15 13:41:32 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2003-06-16 13:33:38 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2003-06-15 13:41:32 +00:00
|
|
|
#include "hash.hh"
|
2003-06-20 10:40:25 +00:00
|
|
|
#include "archive.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
#include "util.hh"
|
|
|
|
#include "eval.hh"
|
|
|
|
#include "values.hh"
|
|
|
|
#include "globals.hh"
|
2003-06-15 13:41:32 +00:00
|
|
|
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
void realise(FState fs)
|
2003-06-15 13:41:32 +00:00
|
|
|
{
|
2003-06-27 14:56:12 +00:00
|
|
|
cout << format("%1% => %2%\n")
|
|
|
|
% printTerm(fs)
|
|
|
|
% printTerm(realiseFState(fs));
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
void realiseFail(FState fs)
|
2003-06-27 09:55:31 +00:00
|
|
|
{
|
|
|
|
try {
|
2003-06-27 13:55:12 +00:00
|
|
|
realiseFState(fs);
|
2003-06-27 09:55:31 +00:00
|
|
|
abort();
|
|
|
|
} catch (Error e) {
|
|
|
|
cout << "error (expected): " << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-16 15:59:23 +00:00
|
|
|
struct MySink : DumpSink
|
|
|
|
{
|
|
|
|
virtual void operator () (const unsigned char * data, unsigned int len)
|
|
|
|
{
|
2003-06-18 14:34:43 +00:00
|
|
|
/* Don't use cout, it's slow as hell! */
|
2003-06-23 13:27:59 +00:00
|
|
|
if (write(STDOUT_FILENO, (char *) data, len) != (ssize_t) len)
|
|
|
|
throw SysError("writing to stdout");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct MySource : RestoreSource
|
|
|
|
{
|
|
|
|
virtual void operator () (const unsigned char * data, unsigned int len)
|
|
|
|
{
|
|
|
|
ssize_t res = read(STDIN_FILENO, (char *) data, len);
|
|
|
|
if (res == -1)
|
|
|
|
throw SysError("reading from stdin");
|
|
|
|
if (res != (ssize_t) len)
|
|
|
|
throw Error("not enough data available on stdin");
|
2003-06-16 15:59:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-06-16 13:33:38 +00: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-06-16 15:59:23 +00:00
|
|
|
/* Dumping. */
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
MySink sink;
|
|
|
|
dumpPath("scratch", sink);
|
|
|
|
cout << (string) hashPath("scratch") << endl;
|
|
|
|
#endif
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
/* Restoring. */
|
2003-06-23 14:40:49 +00:00
|
|
|
#if 0
|
2003-06-23 13:27:59 +00:00
|
|
|
MySource source;
|
|
|
|
restorePath("outdir", source);
|
|
|
|
cout << (string) hashPath("outdir") << endl;
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2003-06-16 13:33:38 +00:00
|
|
|
/* Set up the test environment. */
|
|
|
|
|
|
|
|
mkdir("scratch", 0777);
|
2003-06-15 13:41:32 +00:00
|
|
|
|
2003-06-16 13:33:38 +00:00
|
|
|
string testDir = absPath("scratch");
|
|
|
|
cout << testDir << endl;
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
nixStore = testDir;
|
2003-06-16 13:33:38 +00:00
|
|
|
nixLogDir = testDir;
|
|
|
|
nixDB = testDir + "/db";
|
|
|
|
|
|
|
|
initDB();
|
|
|
|
|
|
|
|
/* Expression evaluation. */
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
#if 0
|
2003-06-27 09:55:31 +00:00
|
|
|
eval(whNormalise,
|
|
|
|
ATmake("Str(\"Hello World\")"));
|
|
|
|
eval(whNormalise,
|
|
|
|
ATmake("Bool(True)"));
|
|
|
|
eval(whNormalise,
|
|
|
|
ATmake("Bool(False)"));
|
|
|
|
eval(whNormalise,
|
|
|
|
ATmake("App(Lam(\"x\", Var(\"x\")), Str(\"Hello World\"))"));
|
|
|
|
eval(whNormalise,
|
|
|
|
ATmake("App(App(Lam(\"x\", Lam(\"y\", Var(\"x\"))), Str(\"Hello World\")), Str(\"Hallo Wereld\"))"));
|
|
|
|
eval(whNormalise,
|
|
|
|
ATmake("App(Lam(\"sys\", Lam(\"x\", [Var(\"x\"), Var(\"sys\")])), Str(\"i686-suse-linux\"))"));
|
|
|
|
|
|
|
|
evalFail(whNormalise,
|
|
|
|
ATmake("Foo(123)"));
|
|
|
|
|
|
|
|
string builder1fn = absPath("./test-builder-1.sh");
|
|
|
|
Hash builder1h = hashFile(builder1fn);
|
|
|
|
|
|
|
|
string fn1 = nixValues + "/builder-1.sh";
|
|
|
|
Expr e1 = ATmake("File(<str>, ExtFile(<str>, <str>), [])",
|
|
|
|
fn1.c_str(),
|
|
|
|
builder1h.c_str(),
|
|
|
|
builder1fn.c_str());
|
|
|
|
eval(fNormalise, e1);
|
|
|
|
|
|
|
|
string fn2 = nixValues + "/refer.txt";
|
|
|
|
Expr e2 = ATmake("File(<str>, Regular(<str>), [<term>])",
|
|
|
|
fn2.c_str(),
|
|
|
|
("I refer to " + fn1).c_str(),
|
|
|
|
e1);
|
|
|
|
eval(fNormalise, e2);
|
|
|
|
|
|
|
|
realise(e2);
|
2003-06-27 13:55:12 +00:00
|
|
|
#endif
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
Hash builder1h;
|
|
|
|
string builder1fn;
|
|
|
|
addToStore("./test-builder-1.sh", builder1fn, builder1h);
|
|
|
|
|
|
|
|
FState fs1 = ATmake(
|
|
|
|
"File(<str>, Hash(<str>), [])",
|
|
|
|
builder1fn.c_str(),
|
|
|
|
((string) builder1h).c_str());
|
2003-06-27 14:56:12 +00:00
|
|
|
realise(fs1);
|
|
|
|
realise(fs1);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
FState fs2 = ATmake(
|
|
|
|
"File(<str>, Hash(<str>), [])",
|
|
|
|
(builder1fn + "_bla").c_str(),
|
|
|
|
((string) builder1h).c_str());
|
2003-06-27 14:56:12 +00:00
|
|
|
realise(fs2);
|
|
|
|
realise(fs2);
|
|
|
|
|
|
|
|
string out1fn = nixStore + "/hello.txt";
|
|
|
|
FState fs3 = ATmake(
|
|
|
|
"Derive(<str>, <str>, [<term>], <str>, [(\"out\", <str>)])",
|
|
|
|
thisSystem.c_str(),
|
|
|
|
builder1fn.c_str(),
|
|
|
|
fs1,
|
|
|
|
out1fn.c_str(),
|
|
|
|
out1fn.c_str());
|
|
|
|
realise(fs3);
|
2003-06-27 13:55:12 +00:00
|
|
|
|
|
|
|
#if 0
|
2003-06-17 15:45:43 +00:00
|
|
|
Expr e1 = ATmake("Exec(Str(<str>), Hash(<str>), [])",
|
2003-06-16 21:01:18 +00:00
|
|
|
thisSystem.c_str(), ((string) builder1).c_str());
|
|
|
|
|
2003-06-27 09:55:31 +00:00
|
|
|
eval(e1);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
Hash builder2 = addValue("./test-builder-2.sh");
|
|
|
|
|
2003-06-16 21:01:18 +00:00
|
|
|
Expr e2 = ATmake(
|
2003-06-17 15:45:43 +00:00
|
|
|
"Exec(Str(<str>), Hash(<str>), [Tup(Str(\"src\"), <term>)])",
|
2003-06-16 21:01:18 +00:00
|
|
|
thisSystem.c_str(), ((string) builder2).c_str(), e1);
|
|
|
|
|
2003-06-27 09:55:31 +00:00
|
|
|
eval(e2);
|
2003-06-17 15:45:43 +00:00
|
|
|
|
2003-06-17 15:47:25 +00:00
|
|
|
Hash h3 = addValue("./test-expr-1.nix");
|
2003-06-17 15:45:43 +00:00
|
|
|
Expr e3 = ATmake("Deref(Hash(<str>))", ((string) h3).c_str());
|
|
|
|
|
2003-06-27 09:55:31 +00:00
|
|
|
eval(e3);
|
2003-06-23 14:40:49 +00:00
|
|
|
|
|
|
|
deleteValue(h3);
|
2003-06-27 09:55:31 +00:00
|
|
|
#endif
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
ATerm bottomOfStack;
|
|
|
|
ATinit(argc, argv, &bottomOfStack);
|
2003-06-15 13:41:32 +00:00
|
|
|
|
2003-06-16 13:33:38 +00:00
|
|
|
try {
|
|
|
|
runTests();
|
|
|
|
} catch (exception & e) {
|
|
|
|
cerr << "error: " << e.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2003-06-15 13:41:32 +00:00
|
|
|
}
|