Skip to content

Commit

Permalink
basic test framework with github action
Browse files Browse the repository at this point in the history
  • Loading branch information
flocks committed Jan 5, 2024
1 parent ceb5590 commit 543ed2a
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 26 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
gsx
test/test
*.o
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 2 additions & 24 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <errno.h>

#include "parse.h"
#include "utils.h"

#define INIT_RESULT_CAPACITY 32
#define MAX_COMMAND_LINE_LENGTH 1024
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#define _GNU_SOURCE // make popen available
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#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;

}
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
char *read_file(const char* file_path);
char *read_command(const char* command);
4 changes: 4 additions & 0 deletions test/result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test/samples/file.tsx:5:<Button variant="primary" size="large">
--
test/samples/file.tsx:9:<Button variant="secondary">
--
13 changes: 13 additions & 0 deletions test/samples/file.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Test = () => {
return (
<Box variant="primary" onClick={() => console.log("test")}>
<div>
<Button variant="primary" size="large">
Button
</Button>
<Button size="large">Button</Button>
<Button variant="secondary">Button</Button>
</div>
</Box>
);
};
61 changes: 61 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#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;
}

0 comments on commit 543ed2a

Please sign in to comment.