Skip to content

Commit 4c024b0

Browse files
committed
refactor pty test into seperate script
1 parent b5b2144 commit 4c024b0

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

Makefile

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,9 @@ all: bin/
1010

1111
.PHONY: test
1212
test:
13-
@tmpdir=$$(mktemp -d); \
14-
cleanup() { pkill -P $$$$; rm -rf $$tmpdir; }; \
15-
trap cleanup EXIT INT TERM; \
16-
diagslave -m tcp -p 5020 & diagslave -m enc -p 5021 & \
17-
go test -run TCP -v .; \
18-
pkill -P $$$$
19-
@tmpdir=$$(mktemp -d); \
20-
cleanup() { pkill -P $$$$; rm -rf $$tmpdir; }; \
21-
trap cleanup EXIT INT TERM; \
22-
socat -d -d pty,raw,echo=0,link=$$tmpdir/rtu0 pty,raw,echo=0,link=$$tmpdir/rtu1 & \
23-
sleep 0.5; \
24-
diagslave -m rtu $$tmpdir/rtu1 & \
25-
go test -run RTU -v .; \
26-
pkill -P $$$$
27-
@tmpdir=$$(mktemp -d); \
28-
cleanup() { pkill -P $$$$; rm -rf $$tmpdir; }; \
29-
trap cleanup EXIT INT TERM; \
30-
socat -d -d pty,raw,echo=0,link=$$tmpdir/ascii0 pty,raw,echo=0,link=$$tmpdir/ascii1 & \
31-
sleep 0.5; \
32-
diagslave -m ascii $$tmpdir/ascii1 & \
33-
go test -run ASCII -v .; \
34-
pkill -P $$$$
13+
./scripts/run-test-with-pty.sh tcp TCP
14+
./scripts/run-test-with-pty.sh rtu RTU
15+
./scripts/run-test-with-pty.sh ascii ASCII
3516
go test -v -count=1 github.com/grid-x/modbus/cmd/modbus-cli
3617

3718
.PHONY: lint

scripts/run-test-with-pty.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
set -e
3+
4+
MODE=$1
5+
TEST_FILTER=$2
6+
7+
tmpdir=$(mktemp -d)
8+
9+
cleanup() {
10+
pkill -P $$ 2>/dev/null || true
11+
rm -rf "$tmpdir"
12+
}
13+
14+
trap cleanup EXIT INT TERM
15+
16+
case "$MODE" in
17+
tcp)
18+
diagslave -m tcp -p 5020 &
19+
diagslave -m enc -p 5021 &
20+
go test -run "$TEST_FILTER" -v .
21+
;;
22+
rtu)
23+
socat -d -d pty,raw,echo=0,link="$tmpdir/pty0" pty,raw,echo=0,link="$tmpdir/pty1" &
24+
sleep 0.5
25+
diagslave -m rtu "$tmpdir/pty1" &
26+
go test -run "$TEST_FILTER" -v .
27+
;;
28+
ascii)
29+
socat -d -d pty,raw,echo=0,link="$tmpdir/pty0" pty,raw,echo=0,link="$tmpdir/pty1" &
30+
sleep 0.5
31+
diagslave -m ascii "$tmpdir/pty1" &
32+
go test -run "$TEST_FILTER" -v .
33+
;;
34+
*)
35+
echo "Usage: $0 {tcp|rtu|ascii} TEST_FILTER"
36+
exit 1
37+
;;
38+
esac

0 commit comments

Comments
 (0)