From 54670ba9e8d90f070bd74a9b1e6a716636498354 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 | 19 ++++++++ .gitignore | 1 + Makefile | 14 ++++-- src/main.c | 26 +---------- src/utils.c | 51 +++++++++++++++++++++ src/utils.h | 2 + test/exclude_one_prop.txt | 2 + test/include_one_exclude_one.txt | 2 + test/one_prop.txt | 4 ++ test/samples/file.tsx | 13 ++++++ test/simple.txt | 6 +++ test/test.c | 76 ++++++++++++++++++++++++++++++++ 12 files changed, 188 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 src/utils.c create mode 100644 src/utils.h create mode 100644 test/exclude_one_prop.txt create mode 100644 test/include_one_exclude_one.txt create mode 100644 test/one_prop.txt create mode 100644 test/samples/file.tsx create mode 100644 test/simple.txt create mode 100644 test/test.c diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..d1c7a3d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,19 @@ +name: Run tests +on: + - push + - pull_request + +jobs: + run-tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: true + - 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..684e09d 100644 --- a/Makefile +++ b/Makefile @@ -9,13 +9,19 @@ $(STATIC_LIB): cd $(TREE_SITTER) && $(MAKE) scanner.o: $(TREE_SITTER_TSX)/src/scanner.c - $(CC) -c $< -o $@ + $(CC) -c $< -o $@ -I$(TREE_SITTER)/lib/include parser.o: $(TREE_SITTER_TSX)/src/parser.c - $(CC) -c $< -o $@ + $(CC) -c $< -o $@ -I$(TREE_SITTER)/lib/include -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/exclude_one_prop.txt b/test/exclude_one_prop.txt new file mode 100644 index 0000000..3b88c6c --- /dev/null +++ b/test/exclude_one_prop.txt @@ -0,0 +1,2 @@ +test/samples/file.tsx:8: + + + + + ); +}; diff --git a/test/simple.txt b/test/simple.txt new file mode 100644 index 0000000..df6aecb --- /dev/null +++ b/test/simple.txt @@ -0,0 +1,6 @@ +test/samples/file.tsx:5: