From 543ed2a1810f4b57fe1b3f851bb820e6764c9c1d Mon Sep 17 00:00:00 2001 From: Florent Teissier Date: Fri, 5 Jan 2024 12:33:49 +0100 Subject: [PATCH] basic test framework with github action --- .github/workflows/test.yml | 17 +++++++++++ .gitignore | 1 + Makefile | 10 +++++-- src/main.c | 26 ++-------------- src/utils.c | 51 +++++++++++++++++++++++++++++++ src/utils.h | 2 ++ test/result.txt | 4 +++ test/samples/file.tsx | 13 ++++++++ test/test.c | 61 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 src/utils.c create mode 100644 src/utils.h create mode 100644 test/result.txt create mode 100644 test/samples/file.tsx create mode 100644 test/test.c diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..c5064d0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,17 @@ +name: Run tests +on: + - push + - pull_request + +jobs: + run-tests: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v4 + - name: setup env + run: | + sudo apt-get update + sudo apt install build-essential + - name: run tests + run: | + make run_test diff --git a/.gitignore b/.gitignore index 935ddd9..0fbe14e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ gsx +test/test *.o \ No newline at end of file diff --git a/Makefile b/Makefile index 2f1b2a1..11cd9cb 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,14 @@ scanner.o: $(TREE_SITTER_TSX)/src/scanner.c parser.o: $(TREE_SITTER_TSX)/src/parser.c $(CC) -c $< -o $@ -gsx: src/parse.h src/parse.c src/main.c scanner.o parser.o $(STATIC_LIB) - $(CC) -Wall -Wextra -std=c11 -pedantic -o gsx $(CGLAGS) src/parse.c src/main.c scanner.o parser.o $(STATIC_LIB) +gsx: src/parse.h src/parse.c src/utils.c src/main.c scanner.o parser.o $(STATIC_LIB) + $(CC) -Wall -Wextra -std=c11 -pedantic -o gsx $(CGLAGS) src/utils.c src/parse.c src/main.c scanner.o parser.o $(STATIC_LIB) + +test/test: gsx src/utils.c test/test.c + $(CC) -Wall -Wextra -o test/test src/utils.c test/test.c + +run_test: test/test + ./test/test clean: rm -f *.o && rm -f main diff --git a/src/main.c b/src/main.c index d7df180..5d9587b 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ #include #include "parse.h" +#include "utils.h" #define INIT_RESULT_CAPACITY 32 #define MAX_COMMAND_LINE_LENGTH 1024 @@ -41,29 +42,6 @@ void free_result(Result *r) { r->size = r->capacity = 0; } -char* readFile(char* file_name) { - FILE *f = fopen(file_name, "rb"); - - if (f == NULL) { - fprintf(stderr, "Couldn't open %s", file_name); - fprintf(stderr, "%d", errno); - - exit(EXIT_FAILURE); - } - if(fseek(f, 0, SEEK_END) != 0) { - fprintf(stderr, "Error reading %s", file_name); - exit(EXIT_FAILURE); - } - long size = ftell(f); - char* source_code = (char *)malloc((size+1) * sizeof(char)); - rewind(f); - - fread(source_code, 1, size, f); - source_code[size] = '\0'; - fclose(f); - - return source_code; -} char* get_node_content(TSNode node, char* source_code) { uint32_t start_offset = ts_node_start_byte(node); @@ -213,7 +191,7 @@ int main(int argc, char** argv) { while (fgets(file_path, sizeof(file_path), cmd)) { file_path[strlen(file_path)-1] = '\0'; - char* source_code = readFile(file_path); + char* source_code = read_file(file_path); Result result_ast = { .items = NULL, .size = 0, .capacity = 0 }; TSTree* tree = build_tree(source_code, file_path, parser, &p, &result_ast); diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..67c2982 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,51 @@ +#define _GNU_SOURCE // make popen available +#include +#include +#include + +#define CHUNK_SIZE 64*100 + +char* read_file(char* file_name) { + FILE *f = fopen(file_name, "rb"); + + if (f == NULL) { + fprintf(stderr, "Couldn't open %s", file_name); + fprintf(stderr, "%d", errno); + + exit(EXIT_FAILURE); + } + if (fseek(f, 0, SEEK_END) != 0) { + fprintf(stderr, "Error reading %s", file_name); + exit(EXIT_FAILURE); + } + long size = ftell(f); + char* content = (char *)malloc((size+1) * sizeof(char)); + rewind(f); + + fread(content, 1, size, f); + content[size] = '\0'; + fclose(f); + + return content; +} + +char* read_command(char* command) { + (void) command; + char *result = (char *)malloc(CHUNK_SIZE * sizeof(char)); + size_t bytes_length = 0; + size_t length = 0; + + FILE *output = popen(command, "r"); + if (output == NULL) { + fprintf(stderr, "Error running command: %s\n", command); + exit(EXIT_FAILURE); + } + while((bytes_length = fread(result, 1, CHUNK_SIZE, output)) > 0) { + length += bytes_length; + result = (char *)realloc(result, (length + 1) * sizeof(char)); + } + result[length] = '\0'; + + return result; + +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..4df45e5 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,2 @@ +char *read_file(const char* file_path); +char *read_command(const char* command); diff --git a/test/result.txt b/test/result.txt new file mode 100644 index 0000000..a209132 --- /dev/null +++ b/test/result.txt @@ -0,0 +1,4 @@ +test/samples/file.tsx:5: + + + + + ); +}; diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..62caa62 --- /dev/null +++ b/test/test.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include + +#include "../src/utils.h" + +typedef const char* Filepath; +typedef const char* Command; +typedef const char* Name; + +typedef struct { + Name name; + Command command; + Filepath file; +} Test; + + +bool run_test(Test* test) { + bool success = true; + char *result = read_command(test->command); + char *expected = read_file(test->file); + + + if (memcmp(result, expected, strlen(result)) == 0) { + printf("%s: OK\n", test->name); + } else { + printf("%s: KO\n", test->name); + printf("Expected: \n"); + printf("%s\n", expected); + printf("Got: \n"); + printf("%s\n", result); + success = false; + } + printf("----------------------------------------\n"); + free(expected); + free(result); + return success; +} + +Test tests[] = { + (Test) { + .name = "Regular", + .command = "./gsx test/samples/ Button.variant", + .file = "./test/result.txt", + } +}; + +int main(void) { + size_t nb_test = sizeof(tests) / sizeof(tests[0]); + printf("Running all tests\n\n"); + int success = 0; + for (size_t i = 0; i < nb_test; ++i) { + if (!run_test(&tests[i])) { + success = 1; + } + } + return success; +} +