Skip to content

Commit 05a8b14

Browse files
committed
Add test target to run tests
1 parent 0cb6984 commit 05a8b14

12 files changed

+100
-44
lines changed

.dockerignore

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
.git
44
.gitignore
5+
56
README.md
6-
bin/
7-
blib/
7+
88
deploy/
99
launcher/
10-
lib/
1110
local/
12-
public/
13-
t/
11+
12+
src/blib/
13+
src/environments/*docker*
14+
src/lib/
15+
src/public/
16+
src/t/
17+
src/var/
18+
src/views/
19+
1420
tools/
15-
var/
16-
views/
21+
1722
.env
1823
*.log
1924
*.swp

Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
FROM metacpan/metacpan-base:latest AS builder
66
SHELL [ "/bin/bash", "-eo", "pipefail", "-c" ]
77

8-
WORKDIR /metacpan-grep-front-end
8+
# copy the cpanfile and cpanfile.snapshot
9+
# from the current directory to the /cpan directory in the image
10+
# and install the dependencies using cpm
11+
# we could then reuse the cpanfile and cpanfile.snapshot for testing
12+
WORKDIR /cpan
913

1014
COPY cpanfile ./
1115
COPY cpanfile.snapshot ./
@@ -33,6 +37,7 @@ ARG APP_ENV=development
3337
# Runtime
3438
ENV APP_ENV=$APP_ENV
3539

40+
# .dockerignore is used to exclude files from the build context
3641
COPY src/ ./
3742

3843
# always expose a consistent port
@@ -45,3 +50,4 @@ RUN chmod +x docker-entrypoint.sh
4550

4651
# Use the dynamic entrypoint
4752
ENTRYPOINT ["./docker-entrypoint.sh"]
53+
CMD ["serve"]

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

2-
.PHONY: all up-dev up-prod up
2+
.PHONY: all up-dev up-prod up test t
33

44
APP_ENV ?= development
55

66
# Add overload config for docker-compose
77
ifeq ($(APP_ENV),development)
88
COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml
9+
else ifeq ($(APP_ENV),test)
10+
COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml:docker-compose.test.yml
911
else
1012
COMPOSE_FILE=docker-compose.yml
1113
endif
@@ -15,10 +17,15 @@ all:
1517

1618
up:
1719
ln -sf config/docker-compose.$(APP_ENV).env .env
18-
APP_ENV=$(APP_ENV) docker compose -f $(subst :, -f ,$(COMPOSE_FILE)) up --build
20+
APP_ENV=$(APP_ENV) docker compose -f $(subst :, -f ,$(COMPOSE_FILE)) up --build --exit-code-from grep
1921

2022
up-dev:
2123
$(MAKE) up APP_ENV=development
2224

2325
up-prod:
24-
$(MAKE) up APP_ENV=production
26+
$(MAKE) up APP_ENV=production
27+
28+
t: test
29+
30+
test:
31+
$(MAKE) up APP_ENV=test

config/docker-compose.test.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
HOST_PORT=5010
2+
PLACKUP_ARGS='-R lib,bin'
3+
CPAN_VOLUME_PATH=../metacpan-cpan-extracted-lite
4+
# only setup for development purpose
5+
SRC_VOLUME_PATH=./src

docker-compose.test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
grep:
3+
command: ["bash", "t/run-tests.sh"]
4+
environment:
5+
- APP_ENV=test

src/docker-entrypoint.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#!/bin/bash -e
22

3-
echo "Starting the application..."
4-
echo "Environment: $APP_ENV"
5-
echo "Host Port: $HOST_PORT"
6-
echo "Plackup Options: $PLACKUP_ARGS"
7-
echo "git binary: $(which git) - $(git --version)"
8-
echo "========================="
9-
echo "Access the application at http://localhost:${HOST_PORT}"
10-
echo "========================="
3+
if [ "$1" = "serve" ]; then
4+
echo "Starting the application..."
5+
echo "Environment: $APP_ENV"
6+
echo "Host Port: $HOST_PORT"
7+
echo "Plackup Options: $PLACKUP_ARGS"
8+
echo "git binary: $(which git) - $(git --version)"
9+
echo "========================="
10+
echo "Access the application at http://localhost:${HOST_PORT}"
11+
echo "========================="
1112

12-
plackup -p 3000 ${PLACKUP_ARGS} bin/app.psgi
13+
plackup -p 3000 ${PLACKUP_ARGS} bin/app.psgi
14+
else
15+
echo "Running custom command: $@"
16+
exec "$@"
17+
fi

src/environments/unit-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ startup_info: 1
2525
grepcpan:
2626
nocache: 0
2727
maxworkers: 2
28-
gitrepo: '/metacpan-cpan-extracted-lite'
28+
gitrepo: '/metacpan-cpan-extracted'
2929
cache:
3030
directory: '/tmp'

src/lib/GrepCpan/Grep.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ sub current_version($self) {
462462
'cache' => $self->config()->{'cache'}->{'version'},
463463
'grep' => eval {
464464
scalar Git::Repository->new(
465-
work_tree => $self->root,
465+
work_tree => $self->config()->{'gitrepo'},
466466
{ git => $self->git_binary }
467467
)->run(qw{rev-parse --short HEAD});
468468
} // '',
@@ -587,6 +587,8 @@ sub _load_cache ( $self, $cache_file ) {
587587

588588
sub _parse_and_check_query_filetype ( $self, $query_filetype, $adjusted_request={} ) {
589589

590+
return unless length $query_filetype;
591+
590592
my $rules = $self->_parse_query_filetype($query_filetype);
591593

592594
my $r = $rules // [];

src/t/GrepCpan-Grep-dosearch.t

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#package grepcpan;
2-
#use Dancer2;
1+
use v5.36;
32

43
use strict;
54
use warnings;
65

6+
local $| = 1;
7+
78
BEGIN {
89
use FindBin;
910
unshift @INC, $FindBin::Bin . "/lib";
@@ -50,7 +51,7 @@ my $config = {
5051
'history_size' => '20'
5152
},
5253
'demo' => '0',
53-
'gitrepo' => '~APPDIR~/../metacpan-cpan-extracted-lite',
54+
'gitrepo' => '/metacpan-cpan-extracted',
5455
'limit' => {
5556
'distros_per_page' => '30',
5657
'files_git_run_bg' => '2000',
@@ -133,6 +134,7 @@ my $query_looks_sane = validator(
133134
field search_in_progress => $is_boolean; # cannot guess the value
134135
field 'time_elapsed' => match(qr{^[0-9]+\.[0-9]+});
135136
field version => D();
137+
field adjusted_request => hash{};
136138

137139
}
138140
}
@@ -147,25 +149,13 @@ my $queries = [
147149
'third page' => { search => 'test', page => 2 },
148150
'fourth page' => { search => 'test', page => 3 },
149151

150-
sub { $is_a_known_distro = 1 }, undef,
151-
'search distro eBay-API' =>
152-
{ search => 'test', search_distro => 'eBay-API' },
152+
# sub { $is_a_known_distro = 1 }, undef,
153+
# 'search distro Try-Tiny' =>
154+
# { search => 'try', search_distro => 'Try-Tiny' },
153155
];
154156

155157
my $iterator = natatime 2, @$queries;
156158

157-
# my $query = $grep->do_search(
158-
# search => 'test',
159-
160-
# ## optional search parameters
161-
# #page => 0,
162-
# #search_distro => $qdistro, # filter on a distribution
163-
# #search_file => $file,
164-
# #filetype => $filetype,
165-
# #caseinsensitive => $qci,
166-
# #list_files => $qls, # not used for now, only impact the view
167-
# );
168-
169159
while ( my ( $name, $opts ) = $iterator->() ) {
170160
if ( ref $name eq 'CODE' ) {
171161

@@ -175,7 +165,7 @@ while ( my ( $name, $opts ) = $iterator->() ) {
175165
}
176166

177167
my $query = $grep->do_search(%$opts);
178-
is $query, $query_looks_sane, $name;
168+
is( $query, $query_looks_sane, $name ) or diag explain $query;
179169
}
180170

181171
done_testing;

src/t/GrepCpan-Grep-workers.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ok -d $tmpdir, "using tmp directory: $tmpdir";
1818

1919
my $config = {
2020
'maxworkers' => '2',
21-
'gitrepo' => '~APPDIR~/../metacpan-cpan-extracted-lite',
21+
'gitrepo' => '/metacpan-cpan-extracted',
2222
'cache' => {
2323
'directory' => $tmpdir,
2424
'version' => "0.$$"

src/t/GrepCpan-Grep.t

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ my $config = {
3131
'history_size' => '20'
3232
},
3333
'demo' => '0',
34-
'gitrepo' => '~APPDIR~/../metacpan-cpan-extracted-lite',
34+
'gitrepo' => '/metacpan-cpan-extracted',
3535
'limit' => {
3636
'distros_per_page' => '30',
3737
'files_git_run_bg' => '2000',
@@ -65,7 +65,15 @@ foreach my $k ( sort keys %$test_methods ) {
6565
ok -x $grep->git_binary, "git is executable";
6666

6767
like $grep->current_version,
68-
qr{^[0-9]+\.[0-9_]+-cache-[0-9]+\.[0-9]+-grep-[0-9a-f]+-cpan-[0-9a-f]+$},
68+
qr{^
69+
[0-9]+\.[0-9_]+
70+
-cache-
71+
[0-9]+\.[0-9]+
72+
-grep-
73+
[0-9a-f]+
74+
-cpan-
75+
[0-9a-f]+
76+
$}xs,
6977
"current_version";
7078

7179
{

src/t/run-tests.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!bash
2+
3+
set -ex
4+
5+
##
6+
## install the extra test dependencies
7+
##
8+
cd /cpan
9+
10+
cpm install -g \
11+
--with-test \
12+
--with-recommends \
13+
--with-develop \
14+
--cpanfile cpanfile
15+
16+
##
17+
## run the tests
18+
##
19+
cd /metacpan-grep-front-end
20+
21+
export TABLE_TERM_SIZE=120
22+
23+
prove -l -Ilib -r -v t

0 commit comments

Comments
 (0)