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

Commit c81fb5c

Browse files
Merge pull request #154 from TanmayPatil105/cleanup
Reduce memory footprint of procfetch
2 parents 3d524c5 + b1f4344 commit c81fb5c

File tree

6 files changed

+83
-89
lines changed

6 files changed

+83
-89
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/Makefile
22
src/config.h
33
src/procfetch
4+
src/test
45
src/Makefile
56
ascii/Makefile
67
debian/control

src/Makefile.in

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
TARGET = procfetch
2-
SRCS = fetch.cpp main.cpp util.cpp
2+
SRCS = fetch.cpp main.cpp
33
OBJS = $(SRCS:.cpp=.o)
44

55
CXX = @CXX@
66
CXXFLAGS = -std=c++17 -Wall -Wextra --pedantic-errors @CXXFLAGS@
77
LIBS = @LIBS@
88
LDFLAGS = -pthread
99

10+
TEST_TARGET = test
11+
TEST_SRCS = test.cpp fetch.cpp
12+
TEST_OBJS = $(TEST_SRCS:.cpp=.o)
13+
TEST_LDFLAGS = $(LDFLAGS) -no-pie
14+
1015
INSTALL = /usr/bin/install -c -D
1116
FORMATTER = clang-format -i
1217
BIN_DIR = @BIN_DIR@
1318

1419
all: $(TARGET)
1520
run: all
1621
./$(TARGET)
17-
check: all
18-
@if test -f not_existence; then \
19-
echo "Error: It is assumed that file 'not_existence' DOES NOT exist." ;\
20-
false ;\
21-
fi
22-
./$(TARGET) -t
22+
build-test: $(TEST_TARGET)
23+
check: build-test
24+
./$(TEST_TARGET)
2325
gcov:
24-
gcov $(SRCS)
26+
gcov $(TEST_TARGET)
2527
clean:
26-
- rm -f $(TARGET) $(OBJS) *.gcov *.gcda *.gcno
28+
- rm -f $(TARGET) $(OBJS) $(TEST_TARGET) $(TEST_OBJS) *.gcov *.gcda *.gcno
2729
install: all
2830
@echo "Installing $(BIN_DIR)/$(TARGET) ..."
2931
$(INSTALL) $(TARGET) "$(BIN_DIR)/$(TARGET)"
3032
uninstall:
3133
- rm "$(BIN_DIR)/$(TARGET)"
3234
format:
33-
$(FORMATTER) $(SRCS) *.h
35+
$(FORMATTER) $(SRCS) $(TEST_TARGET) *.h
3436

3537
$(TARGET): $(OBJS)
3638
$(CXX) -o $@ $(OBJS) $(LIBS) $(LDFLAGS)
39+
$(TEST_TARGET): $(TEST_OBJS)
40+
$(CXX) -o $@ $(TEST_OBJS) $(LIBS) $(TEST_LDFLAGS)
3741
main.o: fetch.h color.h config.h
3842
fetch.o: fetch.h color.h
39-
util.o: fetch.h color.h
43+
test.o: fetch.h color.h
4044

41-
.PHONY: all run check gcov clean docs install uninstall dist format gif
45+
.PHONY: all run check gcov clean docs install uninstall dist format gif build-test

src/fetch.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ string getuser()
2020
auto *p = getpwuid(getuid());
2121
if (p == NULL)
2222
{
23-
throw runtime_error(
24-
"Could not get struct passwd: "s + strerror(errno));
23+
throw runtime_error("Could not get struct passwd: "s + strerror(errno));
2524
}
2625

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

646645
return;
647646
}
648-
649-
void test_getuser()
650-
{
651-
expect(string(getenv("USER")), getuser(), "getuser"s);
652-
}
653-
654-
void test_fetch()
655-
{
656-
test_getuser();
657-
}

src/fetch.h

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,8 @@ void printBattery(string path);
6767

6868
void print(string color, string distro_name);
6969

70-
void test_fetch();
71-
72-
// util.cpp
73-
7470
string getColor(string);
7571

76-
void test_util();
77-
78-
/**
79-
* Tests that want and got are equal.
80-
* Fails with the supplied failure message, file and line then halt.
81-
* @param want
82-
* @param got
83-
* @param msg
84-
* @param file
85-
* @param line
86-
*/
87-
template <typename T>
88-
void expect1(const T &want, const T &got, const string &msg, const char *file,
89-
int line)
90-
{
91-
if (want == got)
92-
return;
93-
94-
if (msg.length() != 0)
95-
cout << file << ":"s << line << ": Error: "s << msg << " want ("s
96-
<< want << "), but got ("s << got << ")"s << endl;
97-
else
98-
cout << file << ":"s << line << ": Error: want ("s << want
99-
<< "), but got ("s << got << ")"s << endl;
100-
101-
exit(1);
102-
}
103-
#define expect(want, got, msg) expect1(want, got, msg, __FILE__, __LINE__)
104-
10572
/**
10673
* A Path represents a path.
10774
*/
@@ -430,7 +397,6 @@ class Context
430397
enum class Mode
431398
{
432399
NORMAL,
433-
EXEC_TEST,
434400
SHOW_VERSION,
435401
};
436402

@@ -446,14 +412,11 @@ class Options
446412
Options(int argc, char *argv[])
447413
{
448414
int opt;
449-
while ((opt = getopt(argc, argv, "ta:d:vb")) != -1)
415+
while ((opt = getopt(argc, argv, "a:d:vb")) != -1)
450416
{
451417

452418
switch (opt)
453419
{
454-
case 't':
455-
mode = Mode::EXEC_TEST;
456-
break;
457420
case 'a':
458421
color_name = string(optarg);
459422
break;

src/main.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ int main(int argc, char *argv[])
2626
case Mode::NORMAL:
2727
// no-op
2828
break;
29-
case Mode::EXEC_TEST:
30-
test_suite();
31-
cout << Crayon{}.green().text("All unit tests passed."s) << endl;
32-
return 0;
3329
case Mode::SHOW_VERSION:
3430
cout << VERSION << endl;
3531
return 0;
@@ -101,9 +97,3 @@ void DisplayInfo(bool show_battery)
10197

10298
cout << endl;
10399
}
104-
105-
void test_suite()
106-
{
107-
test_fetch();
108-
test_util();
109-
}

src/util.cpp renamed to src/test.cpp

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,60 @@
1-
/**
1+
/*
22
* @file
33
*/
4+
#include "color.h"
45
#include "fetch.h"
56

7+
/**
8+
* Tests that want and got are equal.
9+
* Fails with the supplied failure message, file and line then halt.
10+
* @param want
11+
* @param got
12+
* @param msg
13+
* @param file
14+
* @param line
15+
*/
16+
template <typename T>
17+
void expect1(const T &want, const T &got, const string &msg, const char *file,
18+
int line)
19+
{
20+
if (want == got)
21+
return;
22+
23+
if (msg.length() != 0)
24+
cout << file << ":"s << line << ": Error: "s << msg << " want ("s
25+
<< want << "), but got ("s << got << ")"s << endl;
26+
else
27+
cout << file << ":"s << line << ": Error: want ("s << want
28+
<< "), but got ("s << got << ")"s << endl;
29+
30+
exit(1);
31+
}
32+
#define expect(want, got, msg) expect1(want, got, msg, __FILE__, __LINE__)
33+
34+
/*
35+
* .---------------------------------------------.
36+
* | FETCH TESTS |
37+
* '---------------------------------------------'
38+
*/
39+
static void test_getuser()
40+
{
41+
expect(string(getenv("USER")), getuser(), "getuser"s);
42+
}
43+
44+
/*
45+
* Test fetch functions
46+
*/
47+
static void test_fetch()
48+
{
49+
test_getuser();
50+
}
51+
52+
/*
53+
* .---------------------------------------------.
54+
* | UTILS TESTS |
55+
* '---------------------------------------------'
56+
*/
57+
658
static void test_Command()
759
{
860
auto c = Command::exec("ls Makefile"s);
@@ -176,20 +228,6 @@ static void test_Options_full()
176228
testhelper_Options("full", argc, argv, expect, 6); // remains last "arg"
177229
}
178230

179-
static void test_Options_test()
180-
{
181-
int argc = 2;
182-
const char *argv[] = {"procfetch", "-t", NULL};
183-
184-
Options expect;
185-
expect.mode = Mode::EXEC_TEST;
186-
expect.color_name = "def"s;
187-
expect.distro_name = "def"s;
188-
expect.show_battery = false;
189-
190-
testhelper_Options("test", argc, argv, expect, 2);
191-
}
192-
193231
static void test_Options_version()
194232
{
195233
int argc = 2;
@@ -208,7 +246,6 @@ static void test_Options()
208246
{
209247
test_Options_default();
210248
test_Options_full();
211-
test_Options_test();
212249
test_Options_version();
213250
}
214251

@@ -217,7 +254,7 @@ static void test_Options()
217254
* * class Path
218255
* * class Command
219256
*/
220-
void test_util()
257+
static void test_util()
221258
{
222259
test_Path();
223260
test_Command();
@@ -228,3 +265,13 @@ void test_util()
228265
test_Crayon();
229266
test_Options();
230267
}
268+
269+
int main()
270+
{
271+
test_fetch();
272+
test_util();
273+
274+
cout << Crayon{}.green().text("All unit tests passed."s) << endl;
275+
276+
return 0;
277+
}

0 commit comments

Comments
 (0)