1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-23 23:18:05 +11:00

Add shell automated tests

This commit is contained in:
Virgil Dupras 2019-12-12 14:32:47 -05:00
parent 43f4c5200e
commit 9ab292a6d5
11 changed files with 101 additions and 18 deletions

View File

@ -25,6 +25,16 @@ We don't try to emulate real hardware to ease the development of device drivers
because so far, I don't see the advantage of emulation versus running code on because so far, I don't see the advantage of emulation versus running code on
the real thing. the real thing.
By default, the shell initialized itself with a CFS device containing the
contents of `cfsin/` at launch (it's packed on the fly). You can specify an
alternate CFS device file (it has to be packaed already) through the `-f` flag.
By default, the shell runs interactively, but you can also pipe contents through
stdin instead. The contents will be interpreted exactly as if you had typed it
yourself and the result will be spit in stdout (it includes your typed in
contents because the Collapse OS console echoes back every character that is
sent to it.). This feature is useful for automated tests in `tools/tests/shell`.
## zasm ## zasm
`zasm/zasm` is `apps/zasm` wrapped in an emulator. It is quite central to the `zasm/zasm` is `apps/zasm` wrapped in an emulator. It is quite central to the

View File

@ -1,5 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <termios.h> #include <termios.h>
#include "../emul.h" #include "../emul.h"
#include "shell-bin.h" #include "shell-bin.h"
@ -135,12 +136,36 @@ static void iowr_fsaddr(uint8_t val)
} }
} }
int main() int main(int argc, char *argv[])
{ {
FILE *fp = NULL;
while (1) {
int c = getopt(argc, argv, "f:");
if (c < 0) {
break;
}
switch (c) {
case 'f':
fp = fopen(optarg, "r");
if (fp == NULL) {
fprintf(stderr, "Can't open %s\n", optarg);
return 1;
}
break;
default:
fprintf(stderr, "Usage: shell [-f fsdev]\n");
return 1;
}
}
// Setup fs blockdev // Setup fs blockdev
FILE *fp = popen("../cfspack/cfspack cfsin", "r"); if (fp == NULL) {
fp = popen("../cfspack/cfspack cfsin", "r");
if (fp == NULL) {
fprintf(stderr, "Can't initialize filesystem. Leaving blank.\n");
}
}
if (fp != NULL) { if (fp != NULL) {
printf("Initializing filesystem\n"); fprintf(stderr, "Initializing filesystem\n");
int i = 0; int i = 0;
int c = fgetc(fp); int c = fgetc(fp);
while (c != EOF) { while (c != EOF) {
@ -150,19 +175,20 @@ int main()
} }
fsdev_size = i; fsdev_size = i;
pclose(fp); pclose(fp);
} else {
printf("Can't initialize filesystem. Leaving blank.\n");
} }
// Turn echo off: the shell takes care of its own echoing. bool tty = isatty(fileno(stdin));
struct termios termInfo; struct termios termInfo;
if (tcgetattr(0, &termInfo) == -1) { if (tty) {
printf("Can't setup terminal.\n"); // Turn echo off: the shell takes care of its own echoing.
return 1; if (tcgetattr(0, &termInfo) == -1) {
printf("Can't setup terminal.\n");
return 1;
}
termInfo.c_lflag &= ~ECHO;
termInfo.c_lflag &= ~ICANON;
tcsetattr(0, TCSAFLUSH, &termInfo);
} }
termInfo.c_lflag &= ~ECHO;
termInfo.c_lflag &= ~ICANON;
tcsetattr(0, TCSAFLUSH, &termInfo);
Machine *m = emul_init(); Machine *m = emul_init();
@ -182,10 +208,12 @@ int main()
while (running && emul_step()); while (running && emul_step());
printf("Done!\n"); if (tty) {
termInfo.c_lflag |= ECHO; printf("Done!\n");
termInfo.c_lflag |= ICANON; termInfo.c_lflag |= ECHO;
tcsetattr(0, TCSAFLUSH, &termInfo); termInfo.c_lflag |= ICANON;
emul_printdebug(); tcsetattr(0, TCSAFLUSH, &termInfo);
emul_printdebug();
}
return 0; return 0;
} }

View File

@ -2,6 +2,7 @@ EMULDIR = ../emul
.PHONY: run .PHONY: run
run: run:
$(MAKE) -C $(EMULDIR) zasm/zasm runbin/runbin $(MAKE) -C $(EMULDIR) zasm/zasm runbin/runbin shell/shell
cd unit && ./runtests.sh cd unit && ./runtests.sh
cd zasm && ./runtests.sh cd zasm && ./runtests.sh
cd shell && ./runtests.sh

1
tools/tests/shell/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/test.cfs

View File

@ -0,0 +1 @@
Hello Bar!

View File

@ -0,0 +1 @@
Hello Foo!

View File

@ -0,0 +1,5 @@
Collapse OS
> fls
bar
foo
>

View File

@ -0,0 +1 @@
fls

View File

@ -0,0 +1,4 @@
Collapse OS
> print 42
42
>

View File

@ -0,0 +1 @@
print 42

30
tools/tests/shell/runtests.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/sh -e
EMULDIR=../../emul
SHELL=../../emul/shell/shell
replay() {
fn=$1
replayfn=${fn%.*}.expected
ACTUAL=$("${SHELL}" -f test.cfs < "${fn}" 2> /dev/null)
EXPECTED=$(cat ${replayfn})
if [ "$ACTUAL" = "$EXPECTED" ]; then
echo ok
else
echo different. Whole output:
echo "${ACTUAL}"
exit 1
fi
}
../../cfspack/cfspack cfsin > test.cfs
if [ ! -z $1 ]; then
replay $1
exit 0
fi
for fn in *.replay; do
echo "Replaying ${fn}"
replay "${fn}"
done