diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3bdd78f..a255168 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,9 +9,9 @@ build: # instead of calling g++ directly you can also use some build toolkit like make # install the necessary build tools when needed before_script: - - apt update && apt -y install make autoconf cmake + - apt update && apt -y install make cmake python script: - - mkdir build && cd build && cmake .. && make + - mkdir build && cd build && cmake .. && make && make test #artifacts: # paths: # - mybinary diff --git a/CMakeLists.txt b/CMakeLists.txt index 99595cf..3c1eb5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,59 @@ cmake_minimum_required(VERSION 3.7) -project(fish) +project(fish_shell) + +# We need thread support +find_package(Threads REQUIRED) + +# Enable ExternalProject CMake module +include(ExternalProject) + +# Download and install GoogleTest +ExternalProject_Add( + gtest + URL https://github.com/google/googletest/archive/master.zip + PREFIX ${CMAKE_CURRENT_BINARY_DIR}/gtest + # Disable install step + INSTALL_COMMAND "" +) + +# Get GTest source and binary directories from CMake project +ExternalProject_Get_Property(gtest source_dir binary_dir) + +# Create a libgtest target to be used as a dependency by test programs +add_library(libgtest IMPORTED STATIC GLOBAL) +add_dependencies(libgtest gtest) + +# Set libgtest properties +set_target_properties(libgtest PROPERTIES + "IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a" + "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}" + ) + +# Create a libgmock target to be used as a dependency by test programs +add_library(libgmock IMPORTED STATIC GLOBAL) +add_dependencies(libgmock gtest) + +# Set libgmock properties +set_target_properties(libgmock PROPERTIES + "IMPORTED_LOCATION" "${binary_dir}/googlemock/libgmock.a" + "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}" + ) + +include_directories( + "${source_dir}/googletest/include" + "${source_dir}/googlemock/include" +) set(CMAKE_C_STANDARD 99) -set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra") +set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") -set(SOURCE_FILES main.c fish_shell/fish_core.c fish_shell/fish_core.h fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_types.h fish_shell/fish_utils.c fish_shell/fish_utils.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h) -add_executable(fish ${SOURCE_FILES}) +add_subdirectory(fish_shell) +add_subdirectory(fish_shell_tests) + + +enable_testing() + +add_test(NAME fish_tests + COMMAND fish_tests +) diff --git a/fish_shell/CMakeLists.txt b/fish_shell/CMakeLists.txt new file mode 100644 index 0000000..272f384 --- /dev/null +++ b/fish_shell/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.7) +project(fish_shell) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow") + +file(GLOB SOURCE_FILES *) +add_executable(fish ${SOURCE_FILES}) \ No newline at end of file diff --git a/fish_shell/fish_commands.c b/fish_shell/fish_commands.c index 96dc9b8..a103451 100644 --- a/fish_shell/fish_commands.c +++ b/fish_shell/fish_commands.c @@ -7,9 +7,9 @@ /* Necessary global variables */ char * builtinCommandsStr[] = { - "cd", - "help", - "exit" + (char *) "cd", + (char *) "help", + (char *) "exit" }; diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index 43e4d18..8eb5213 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -23,7 +23,7 @@ void fishLoop(Settings * settings){ printf("%s", settings->PS1); line = fishReadLine(); - splited = split(line, FISH_TOKENS); + splited = split(line, (char*) FISH_TOKENS); splited = fishExpand(splited); array = wordListToWordArray(splited); @@ -72,7 +72,7 @@ WordList * split(char *string, char *separator){ char *fishReadLine() { size_t bufferSize = FISH_BUFFER_SIZE; int position = 0; - char *line = malloc(sizeof(char*) * bufferSize); + char *line = (char*) malloc(sizeof(char*) * bufferSize); int c; if (line == NULL){ @@ -96,7 +96,7 @@ char *fishReadLine() { if ((size_t) position > bufferSize){ bufferSize+=bufferSize; - line = realloc(line, bufferSize); + line = (char*) realloc(line, bufferSize); if (line == NULL){ crash(); } diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index a018a8f..7132ec2 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -10,10 +10,10 @@ void crash(){ char *crashErrors[] = { - "fish: Fucking malloc always returning NULL pointer !", - "fish: Error allocating fucking pointer !", - "fish: C language exploding again", - "fish: It's not you're fault for this time" + (char *) "fish: Fucking malloc always returning NULL pointer !", + (char *) "fish: Error allocating fucking pointer !", + (char *) "fish: C language exploding again", + (char *) "fish: It's not you're fault for this time" }; int picked = 0; @@ -28,9 +28,9 @@ char *getInsult(){ static int init = 0; int picked = 0; char *insults[] = { - "Apprend à écrire crétin !", - "Bolos !", - "Mois aussi je sais écrire de la merde, pourtant je le fait pas !" + (char *) "Apprend à écrire crétin !", + (char *) "Bolos !", + (char *) "Mois aussi je sais écrire de la merde, pourtant je le fait pas !" }; if (!init){ srand((unsigned int) time(NULL)); @@ -67,19 +67,19 @@ WordList *createWordList() { } void addWordList(WordList *list, char *word) { - WordListElement *new = (WordListElement*) malloc(sizeof(WordListElement)); - if (new == NULL) crash(); + WordListElement *newElement = (WordListElement*) malloc(sizeof(WordListElement)); + if (newElement == NULL) crash(); else { - new->next = NULL; - new->previous = list->last; + newElement->next = NULL; + newElement->previous = list->last; if (list->size == 0){ - list->first = new; - list->last = new; + list->first = newElement; + list->last = newElement; } else { - list->last->next = new; - list->last = new; + list->last->next = newElement; + list->last = newElement; } - new->word = strdup(word); + newElement->word = strdup(word); list->size++; } } diff --git a/main.c b/fish_shell/main.c similarity index 68% rename from main.c rename to fish_shell/main.c index 8c76cae..2738a9b 100644 --- a/main.c +++ b/fish_shell/main.c @@ -1,6 +1,6 @@ #include -#include "fish_shell/fish_core.h" -#include "fish_shell/fish_types.h" +#include "fish_core.h" +#include "fish_types.h" int main() { /* todo load config file */ diff --git a/fish_shell_tests/CMakeLists.txt b/fish_shell_tests/CMakeLists.txt new file mode 100644 index 0000000..72e4047 --- /dev/null +++ b/fish_shell_tests/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.7) +project(fish_shell_tests) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + +include_directories( + "${source_dir}/googletest/include" + "${source_dir}/googlemock/include" +) + +file(GLOB SOURCE_FILES_TESTS *) + +add_executable(fish_tests ${SOURCE_FILES_TESTS}) + +target_link_libraries(fish_tests libgtest libgmock) + diff --git a/fish_shell_tests/FishCoreTests.cpp b/fish_shell_tests/FishCoreTests.cpp new file mode 100644 index 0000000..4ef96b8 --- /dev/null +++ b/fish_shell_tests/FishCoreTests.cpp @@ -0,0 +1,36 @@ +// +// Created by Antoine Bartuccio on 15/05/2017. +// + +#include "FishCoreTests.h" +#include "../fish_shell/fish_core.c" +#include "../fish_shell/fish_utils.c" +#include "../fish_shell/fish_commands.c" +#include "../fish_shell/fish_globbing.c" + + +//TEST_F(FishCoreTests, split){ +// ASSERT_TRUE(true); +//} + +TEST(command_split, split){ + char input[] = "git push --force"; + char *output[] = { + (char *) "git", + (char *) "push", + (char *) "--force" + }; + WordList *list = split(input, (char *) FISH_TOKENS); + WordListElement *current = list->first; + int i = 0; + + ASSERT_FALSE(current == NULL); + while(current != NULL){ + ASSERT_STREQ(current->word, output[i]); + current = current->next; + i++; + } + + freeWordList(list); + +} diff --git a/fish_shell_tests/FishCoreTests.h b/fish_shell_tests/FishCoreTests.h new file mode 100644 index 0000000..45ce012 --- /dev/null +++ b/fish_shell_tests/FishCoreTests.h @@ -0,0 +1,15 @@ +// +// Created by Antoine Bartuccio on 15/05/2017. +// + +#ifndef FISH_FISH_CORE_TESTS_H +#define FISH_FISH_CORE_TESTS_H + +#include + +class FishCoreTests : public ::testing::Test { + void SetUp() {} + void TearDown(){} +}; + +#endif //FISH_FISH_CORE_TESTS_H diff --git a/fish_shell_tests/main.cpp b/fish_shell_tests/main.cpp new file mode 100644 index 0000000..c2657e9 --- /dev/null +++ b/fish_shell_tests/main.cpp @@ -0,0 +1,10 @@ +// +// Created by Antoine Bartuccio on 15/05/2017. +// + +#include "gtest/gtest.h" + +int main(int argc, char **argv){ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}