Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #154 from TanmayPatil105/cleanup
Browse files Browse the repository at this point in the history
Reduce memory footprint of procfetch
  • Loading branch information
TanmayPatil105 authored Jun 8, 2024
2 parents 3d524c5 + b1f4344 commit c81fb5c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/Makefile
src/config.h
src/procfetch
src/test
src/Makefile
ascii/Makefile
debian/control
Expand Down
28 changes: 16 additions & 12 deletions src/Makefile.in
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
TARGET = procfetch
SRCS = fetch.cpp main.cpp util.cpp
SRCS = fetch.cpp main.cpp
OBJS = $(SRCS:.cpp=.o)

CXX = @CXX@
CXXFLAGS = -std=c++17 -Wall -Wextra --pedantic-errors @CXXFLAGS@
LIBS = @LIBS@
LDFLAGS = -pthread

TEST_TARGET = test
TEST_SRCS = test.cpp fetch.cpp
TEST_OBJS = $(TEST_SRCS:.cpp=.o)
TEST_LDFLAGS = $(LDFLAGS) -no-pie

INSTALL = /usr/bin/install -c -D
FORMATTER = clang-format -i
BIN_DIR = @BIN_DIR@

all: $(TARGET)
run: all
./$(TARGET)
check: all
@if test -f not_existence; then \
echo "Error: It is assumed that file 'not_existence' DOES NOT exist." ;\
false ;\
fi
./$(TARGET) -t
build-test: $(TEST_TARGET)
check: build-test
./$(TEST_TARGET)
gcov:
gcov $(SRCS)
gcov $(TEST_TARGET)
clean:
- rm -f $(TARGET) $(OBJS) *.gcov *.gcda *.gcno
- rm -f $(TARGET) $(OBJS) $(TEST_TARGET) $(TEST_OBJS) *.gcov *.gcda *.gcno
install: all
@echo "Installing $(BIN_DIR)/$(TARGET) ..."
$(INSTALL) $(TARGET) "$(BIN_DIR)/$(TARGET)"
uninstall:
- rm "$(BIN_DIR)/$(TARGET)"
format:
$(FORMATTER) $(SRCS) *.h
$(FORMATTER) $(SRCS) $(TEST_TARGET) *.h

$(TARGET): $(OBJS)
$(CXX) -o $@ $(OBJS) $(LIBS) $(LDFLAGS)
$(TEST_TARGET): $(TEST_OBJS)
$(CXX) -o $@ $(TEST_OBJS) $(LIBS) $(TEST_LDFLAGS)
main.o: fetch.h color.h config.h
fetch.o: fetch.h color.h
util.o: fetch.h color.h
test.o: fetch.h color.h

.PHONY: all run check gcov clean docs install uninstall dist format gif
.PHONY: all run check gcov clean docs install uninstall dist format gif build-test
13 changes: 1 addition & 12 deletions src/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ string getuser()
auto *p = getpwuid(getuid());
if (p == NULL)
{
throw runtime_error(
"Could not get struct passwd: "s + strerror(errno));
throw runtime_error("Could not get struct passwd: "s + strerror(errno));
}

return p->pw_name;
Expand Down Expand Up @@ -645,13 +644,3 @@ void print(string color_name, string distro_name)

return;
}

void test_getuser()
{
expect(string(getenv("USER")), getuser(), "getuser"s);
}

void test_fetch()
{
test_getuser();
}
39 changes: 1 addition & 38 deletions src/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,8 @@ void printBattery(string path);

void print(string color, string distro_name);

void test_fetch();

// util.cpp

string getColor(string);

void test_util();

/**
* Tests that want and got are equal.
* Fails with the supplied failure message, file and line then halt.
* @param want
* @param got
* @param msg
* @param file
* @param line
*/
template <typename T>
void expect1(const T &want, const T &got, const string &msg, const char *file,
int line)
{
if (want == got)
return;

if (msg.length() != 0)
cout << file << ":"s << line << ": Error: "s << msg << " want ("s
<< want << "), but got ("s << got << ")"s << endl;
else
cout << file << ":"s << line << ": Error: want ("s << want
<< "), but got ("s << got << ")"s << endl;

exit(1);
}
#define expect(want, got, msg) expect1(want, got, msg, __FILE__, __LINE__)

/**
* A Path represents a path.
*/
Expand Down Expand Up @@ -430,7 +397,6 @@ class Context
enum class Mode
{
NORMAL,
EXEC_TEST,
SHOW_VERSION,
};

Expand All @@ -446,14 +412,11 @@ class Options
Options(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "ta:d:vb")) != -1)
while ((opt = getopt(argc, argv, "a:d:vb")) != -1)
{

switch (opt)
{
case 't':
mode = Mode::EXEC_TEST;
break;
case 'a':
color_name = string(optarg);
break;
Expand Down
10 changes: 0 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ int main(int argc, char *argv[])
case Mode::NORMAL:
// no-op
break;
case Mode::EXEC_TEST:
test_suite();
cout << Crayon{}.green().text("All unit tests passed."s) << endl;
return 0;
case Mode::SHOW_VERSION:
cout << VERSION << endl;
return 0;
Expand Down Expand Up @@ -101,9 +97,3 @@ void DisplayInfo(bool show_battery)

cout << endl;
}

void test_suite()
{
test_fetch();
test_util();
}
81 changes: 64 additions & 17 deletions src/util.cpp → src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
/**
/*
* @file
*/
#include "color.h"
#include "fetch.h"

/**
* Tests that want and got are equal.
* Fails with the supplied failure message, file and line then halt.
* @param want
* @param got
* @param msg
* @param file
* @param line
*/
template <typename T>
void expect1(const T &want, const T &got, const string &msg, const char *file,
int line)
{
if (want == got)
return;

if (msg.length() != 0)
cout << file << ":"s << line << ": Error: "s << msg << " want ("s
<< want << "), but got ("s << got << ")"s << endl;
else
cout << file << ":"s << line << ": Error: want ("s << want
<< "), but got ("s << got << ")"s << endl;

exit(1);
}
#define expect(want, got, msg) expect1(want, got, msg, __FILE__, __LINE__)

/*
* .---------------------------------------------.
* | FETCH TESTS |
* '---------------------------------------------'
*/
static void test_getuser()
{
expect(string(getenv("USER")), getuser(), "getuser"s);
}

/*
* Test fetch functions
*/
static void test_fetch()
{
test_getuser();
}

/*
* .---------------------------------------------.
* | UTILS TESTS |
* '---------------------------------------------'
*/

static void test_Command()
{
auto c = Command::exec("ls Makefile"s);
Expand Down Expand Up @@ -176,20 +228,6 @@ static void test_Options_full()
testhelper_Options("full", argc, argv, expect, 6); // remains last "arg"
}

static void test_Options_test()
{
int argc = 2;
const char *argv[] = {"procfetch", "-t", NULL};

Options expect;
expect.mode = Mode::EXEC_TEST;
expect.color_name = "def"s;
expect.distro_name = "def"s;
expect.show_battery = false;

testhelper_Options("test", argc, argv, expect, 2);
}

static void test_Options_version()
{
int argc = 2;
Expand All @@ -208,7 +246,6 @@ static void test_Options()
{
test_Options_default();
test_Options_full();
test_Options_test();
test_Options_version();
}

Expand All @@ -217,7 +254,7 @@ static void test_Options()
* * class Path
* * class Command
*/
void test_util()
static void test_util()
{
test_Path();
test_Command();
Expand All @@ -228,3 +265,13 @@ void test_util()
test_Crayon();
test_Options();
}

int main()
{
test_fetch();
test_util();

cout << Crayon{}.green().text("All unit tests passed."s) << endl;

return 0;
}

0 comments on commit c81fb5c

Please sign in to comment.