Skip to content

Instantly share code, notes, and snippets.

@sagebind
Created April 28, 2015 23:12
Show Gist options
  • Save sagebind/0869a255d9d611cf7718 to your computer and use it in GitHub Desktop.
Save sagebind/0869a255d9d611cf7718 to your computer and use it in GitHub Desktop.
Install CppUnit on UW-Whitewater's servers
#!/bin/bash
# set up paths and colors
BOLD=`tput bold`
GREEN=`tput setaf 2`
NC=`tput sgr0`
LOCAL_PREFIX=$HOME/.usr
LOCAL_SRC=$LOCAL_PREFIX/src
PROJECT_DIR=`pwd`
CPPUNIT_PATH=$PROJECT_DIR/cppunit
# use local autotools
export PATH=$LOCAL_PREFIX/bin:$PATH
# check if we need to install autoconf
if [ ! -f $LOCAL_PREFIX/bin/autoconf ]; then
echo "${BOLD}Local Autoconf not found. Installing now.${NC}"
mkdir -p $LOCAL_SRC
echo Installing Autoconf 2.69 to \""$LOCAL_PREFIX/bin/autoconf"\"...
# install autoconf 2.69
cd $LOCAL_SRC
rm -rf autoconf-2.69 autoconf-2.69.tar.gz
wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar xf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=$LOCAL_PREFIX
make
make install
echo
else
echo "${BOLD}Using Autoconf version found at \"$LOCAL_PREFIX/bin/autoconf\"${NC}"
fi
# check if we need to install automake
if [ ! -f $LOCAL_PREFIX/bin/automake ]; then
echo "${BOLD}Local Automake not found. Installing now.${NC}"
mkdir -p $LOCAL_SRC
echo Installing Automake 1.15 to \""$LOCAL_PREFIX/bin/automake"\"...
# install automake 1.15
cd $LOCAL_SRC
rm -rf automake-1.15 automake-1.15.tar.gz
wget http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz
tar xf automake-1.15.tar.gz
cd automake-1.15
autoconf
./configure --prefix=$LOCAL_PREFIX
make
make install
echo
else
echo "${BOLD}Using Automake version found at \"$LOCAL_PREFIX/bin/automake\"${NC}"
fi
# install cppunit
echo "${BOLD}Installing CppUnit to \"$CPPUNIT_PATH\"...${NC}"
cd $PROJECT_DIR
git clone git://anongit.freedesktop.org/git/libreoffice/cppunit/
cd cppunit
./autogen.sh
./configure --prefix="$CPPUNIT_PATH/inst"
make
make check
make install
echo
cd $PROJECT_DIR
# create Makefile
echo Creating useful Makefile...
cd ..
cat << 'EOF' > Makefile
SRC_FILES = $(wildcard *.cpp)
OBJ_FILES = $(patsubst %.cpp, %.o, $(SRC_FILES))
LD_FLAGS = -Wl,-rpath,./cppunit/inst/lib -L ./cppunit/inst/lib/ -lm -ldl -lcppunit
CC_FLAGS = -Wall -c -I. -I./cppunit/inst/include
all: RunTests
test: RunTests
./RunTests
RunTests: $(OBJ_FILES)
g++ $(LD_FLAGS) -o $@ $(OBJ_FILES) $(LIB_FILES)
%.o: %.cpp
g++ $(CC_FLAGS) -c $< -o $@
clean:
rm -f $(OBJ_FILES) RunTests
EOF
# create some template source files
echo Creating sample unit test files...
cat << 'EOF' > TestRunner.cpp
#include <iostream>
#include <cppunit/TestSuite.h>
#include <cppunit/ui/text/TestRunner.h>
#include "TestClass.h"
using namespace std;
int main() {
CppUnit::TextUi::TestRunner runner;
runner.addTest(TestClass::suite());
runner.run();
return 0;
}
EOF
cat << 'EOF' > TestClass.h
#include <cppunit/TestFixture.h>
#include <cppunit/TestAssert.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include <cppunit/TestCase.h>
using namespace std;
class TestClass : public CppUnit::TestFixture {
public:
static CppUnit::Test *suite() {
CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite("TestClass");
suiteOfTests->addTest(new CppUnit::TestCaller<TestClass>("[Test Name]",
&TestClass::testName));
return suiteOfTests;
}
void testName();
};
EOF
cat << 'EOF' > TestClass.cpp
#include <iostream>
#include "TestClass.h"
using namespace std;
void TestClass::testName()
{
cout << "Enter your tests here!" << endl;
}
EOF
# we're done!
echo
echo "${BOLD}${GREEN}Install complete!${NC}"
echo "${BOLD}To compile your code, use \"${NC}make${BOLD}\"."
echo "To run all tests, use \"${NC}make test${BOLD}\"."
echo "Be sure to add each test method to its class's \"${NC}suite()${BOLD}\" method and each test class to \"${NC}TestRunner.cpp${BOLD}\"."
echo $NC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment