Skip to content

Commit

Permalink
[filemanager] Add a small TDD step
Browse files Browse the repository at this point in the history
  • Loading branch information
thoni56 committed Feb 10, 2023
1 parent ab047f8 commit e739e52
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/TODO.org
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ Some useful commands:
* TODO Get a list of files from compile_commands.json
* TODO If no compile_commands.json use all *.c files in current directory
* TODO Take one argument which is the directory to 'cd' to before operating
* TODO
* TODO Option to prune a directory (tree)
Any file in it will not be considered even if it they are included,
handy for `/usr` where files "never" change so it is unnecessary to
check their time stamps

139 changes: 139 additions & 0 deletions src/fileio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "fileio.h"

#include <unistd.h>
#include <sys/stat.h>

#include "log.h"
#include "head.h"


bool exists(char *path) {
struct stat st;
int rc = stat(path, &st);
return rc == 0;
}

FILE *openFile(char *fileName, char *modes) {
return fopen(fileName, modes);
}

int closeFile(FILE *file) {
return fclose(file);
}

void createDirectory(char *dirname) {
struct stat st;

if (exists(dirname)) {
stat(dirname, &st);
if (S_ISDIR(st.st_mode))
return;
removeFile(dirname);
}
#ifdef __WIN32__
mkdir(dirname);
#else
mkdir(dirname, 0777);
#endif
}


void recursivelyCreateFileDirIfNotExists(char *fpath) {
char *p;
int ch, len;
bool loopFlag = true;

/* Check each level from the deepest, stop when it exists */
len = strlen(fpath);
for (p=fpath+len; p>fpath && loopFlag; p--) {
if (*p!=FILE_PATH_SEPARATOR)
continue;
ch = *p; *p = 0; /* Truncate here, remember the char */
if (directoryExists(fpath)) {
loopFlag=false;
}
*p = ch; /* Restore the char */
}
/* Create each of the remaining levels */
for (p+=2; *p; p++) {
if (*p!=FILE_PATH_SEPARATOR)
continue;
ch = *p; *p = 0;
createDirectory(fpath);
*p = ch;
}
}


void removeFile(char *fileName) {
log_trace("removing file '%s'", fileName);
unlink(fileName);
}

int fileStatus(char *path, struct stat *statP) {
struct stat st;
int return_value;

return_value = stat(path, &st); /* Returns 0 on success */
if (statP != NULL)
*statP = st;
return return_value;
}

time_t fileModificationTime(char *path) {
struct stat st;
if (fileStatus(path, &st) !=0)
return 0; /* File not found? */
return st.st_mtime;
}

size_t fileSize(char *path) {
struct stat st;
if (fileStatus(path, &st) !=0)
return 0; /* File not found? */
return st.st_size;
}

bool isDirectory(char *path) {
return directoryExists(path);
}

bool directoryExists(char *path) {
struct stat st;
int statResult;

statResult = stat(path, &st);
return statResult==0 && S_ISDIR(st.st_mode);
}

bool fileExists(char *fullPath) {
struct stat st;
int statResult;

statResult = stat(fullPath, &st);
return statResult==0 && S_ISREG(st.st_mode);
}

size_t readFile(FILE *file, void *buffer, size_t size, size_t count) {
return fread(buffer, size, count, file);
}

size_t writeFile(FILE *file, void *buffer, size_t size, size_t count) {
return fwrite(buffer, size, count, file);
}

int readChar(FILE *file) {
return getc(file);
}

char *getEnv(const char *variable) {
return getenv(variable);
}

char *getCwd(char *buffer, size_t size) {
return getcwd(buffer, size);
}

const char **getFilesInCurrentDirectory(void) {
return NULL;
}
29 changes: 29 additions & 0 deletions src/fileio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef FILEIO_H_INCLUDED
#define FILEIO_H_INCLUDED

#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>


extern bool exists(char *path);
extern int fileStatus(char *path, struct stat *statbuf);
extern time_t fileModificationTime(char *path);
extern size_t fileSize(char *path);
extern FILE *openFile(char *fileName, char *modes);
extern int closeFile(FILE *file);
extern void createDirectory(char *dirname);
extern void recursivelyCreateFileDirIfNotExists(char *fpath);
extern void removeFile(char *dirname);
extern bool isDirectory(char *fullPath);
extern bool directoryExists(char *fullPath);
extern bool fileExists(char *fullPath);
extern size_t readFile(FILE *file, void *buffer, size_t size, size_t count);
extern size_t writeFile(FILE *file, void *buffer, size_t size, size_t count);
extern int readChar(FILE *file);
extern const char **getFilesInCurrentDirectory(void);

extern char *getEnv(const char *variable);
extern char *getCwd(char *buf, size_t size);

#endif
72 changes: 72 additions & 0 deletions src/fileio.mock
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* -*- c -*-*/
#include <cgreen/mocks.h>
#include "fileio.h"


const char **getFilesInCurrentDirectory(void) {
return (const char **)mock();
}

bool isDirectory(char *fullPath) {
return (bool)mock(fullPath);
}

bool directoryExists(char *fullPath) {
return (bool)mock(fullPath);
}

bool fileExists(char *fullPath) {
return (bool)mock(fullPath);
}

int fileStatus(char *path, struct stat *statbuf) {
return (int)mock(path, statbuf);
}

time_t fileModificationTime(char *path) {
return (time_t)mock(path);
}

size_t fileSize(char *path) {
return (size_t)mock(path);
}

FILE *openFile(char *fileName, char *modes) {
return (FILE *)mock(fileName, modes);
}

int closeFile(FILE *file) {
return (int)mock(file);
}

void createDirectory(char *dirName) {
mock(dirName);
}

void recursivelyCreateFileDirIfNotExists(char *fpath) {
mock(fpath);
}

void removeFile(char *fileName) {
mock(fileName);
}

size_t readFile(FILE *file, void *buffer, size_t size, size_t count) {
return (size_t)mock(buffer, size, count, file);
}

size_t writeFile(FILE *file, void *buffer, size_t size, size_t count) {
return (size_t)mock(buffer, size, count, file);
}

int readChar(FILE *file) {
return (int)mock(file);
}

char *getEnv(const char *variable) {
return (char *)mock(variable);
}

char *getCwd(char *buffer, size_t size) {
return (char *)mock(buffer, size);
}
11 changes: 11 additions & 0 deletions src/filemanager.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include "filemanager.h"

#include <stdlib.h>

#include "common.h"
#include "fileio.h"


FileTable getFilesFromCompilationDatabase(void) {
const char **fileNameTable = getFilesInCurrentDirectory();
UNUSED fileNameTable;

FileTable fileTable = (FileTable)malloc(sizeof(FileTableElement));
UNUSED fileTable;

return NULL;
}
5 changes: 4 additions & 1 deletion src/filemanager_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#include "filemanager.h"

#include "common.h"
#include "fileio.mock"



Describe(Filemananger);
Expand All @@ -15,6 +16,8 @@ AfterEach(Filemananger) {}


Ensure(Filemananger, will_create_an_empty_filetable_for_an_empty_directory) {
const char *fileNameList[] = {NULL};
expect(getFilesInCurrentDirectory, will_return(fileNameList));
FileTable fileTable = getFilesFromCompilationDatabase();
assert_that(fileTable, is_null);
}
2 changes: 1 addition & 1 deletion src/sources.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MODULES = main dispatcher exports clang_adaptor indexer includes references filemanager
MODULES = main dispatcher exports clang_adaptor indexer includes references filemanager fileio

OBJDIR = .objects
SRCS = ${MODULES:=.c}
Expand Down

0 comments on commit e739e52

Please sign in to comment.