Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Jun 10, 2024
1 parent 36db03f commit e10b1c8
Show file tree
Hide file tree
Showing 23 changed files with 376 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ jobs:
run: scripts/chkfmt

- name: tests
run: scripts/tests
run: |
scripts/tests
scripts/run_test_prep
scripts/run_tests
- name: xbuild
run: scripts/xbuild
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ jobs:
run: scripts/chkfmt

- name: tests
run: scripts/tests
run: |
scripts/tests
scripts/run_test_prep
scripts/run_tests
- name: xbuild
run: version=${GITHUB_REF#$"refs/tags/v"} scripts/xbuild
Expand Down
3 changes: 3 additions & 0 deletions scripts/build
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash
set -eE -o pipefail

script_dir=$(dirname "$(realpath "$0")")
cd $script_dir/..

version=$(git describe --tags --always)
go build -ldflags="-X main.appVersion=$version" ./cmd/zfind
3 changes: 3 additions & 0 deletions scripts/chkfmt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash
set -eE -o pipefail

script_dir=$(dirname "$(realpath "$0")")
cd $script_dir/..

res="$(gofmt -l . 2>&1)"

if [ -n "$res" ]; then
Expand Down
3 changes: 3 additions & 0 deletions scripts/lint
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash
set -eE -o pipefail

script_dir=$(dirname "$(realpath "$0")")
cd $script_dir/..

go vet -structtag=false -composites=false ./...
23 changes: 23 additions & 0 deletions scripts/run_test_prep
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

root="/tmp/zfind"

go run scripts/run_test_prep.go

cd $root/root/time
zip -r ../day/time.zip *
7z a ../year/time.7z *

cd $root/root/thing
tar -cvf ../way/thing.tar *
tar -czvf ../year/thing.tar.gz *
tar -czvf ../year/thing.tgz *
tar -cjvf ../people/thing.tar.bz2 *
tar -cjvf ../people/thing.tbz2 *

cd $root/root
rm -rf $root/root/time $root/root/thing
mv $root/root/people $root/people
ln -s ../people people

find -L | wc -l
90 changes: 90 additions & 0 deletions scripts/run_test_prep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)

var (
startList = []string{"time", "year", "people", "way", "day", "thing"}
wordList = []string{"life", "world", "school", "state", "family", "student", "group", "country", "problem", "hand", "part", "place", "case", "week", "company", "system", "program", "work", "government", "number", "night", "point", "home", "water", "room", "mother", "area", "money", "story", "fact", "month", "lot", "right", "study", "book", "eye", "job", "word", "business", "issue", "side", "kind", "head", "house", "service", "friend", "father", "power", "hour", "game", "line", "end", "member", "law", "car", "city", "community", "name", "president", "team", "minute", "idea", "kid", "body", "information", "back", "face", "others", "level", "office", "door", "health", "person", "art", "war", "history", "party", "result", "change", "morning", "reason", "research", "moment", "air", "teacher", "force", "education"}
extList = []string{"txt", "md", "pdf", "jpg", "jpeg", "png", "mp4", "mp3", "csv"}
startDate = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
endDate = time.Date(2024, 12, 1, 0, 0, 0, 0, time.UTC)
dateList = []time.Time{}
wordIdx = 0
extIdx = 0
dateIdx = 0
)

func nextWord() string {
word := wordList[wordIdx%len(wordList)]
wordIdx++
return word
}

func nextExt() string {
ext := extList[extIdx%len(extList)]
extIdx++
return ext
}

func setDate(filename string, r int) {
date := dateList[dateIdx%len(dateList)]
m := 17 * dateIdx / len(dateList)
date = date.Add(time.Duration(m) * time.Hour)
dateIdx++
os.Chtimes(filename, date, date)
}

func genFile(dir string, a int) {
os.MkdirAll(dir, 0755)
for i := 1; i <= 5; i++ {
size := a*i*wordIdx*100 + extIdx
file := nextWord() + "-" + nextWord()

if i%3 == 0 {
file += "-" + nextWord()
}

file += "." + nextExt()
path := filepath.Join(dir, file)
ioutil.WriteFile(path, make([]byte, size), 0644)
setDate(path, size*size)
}
}

func genDir(root string) {
for _, start := range startList {

for i := 1; i <= 5; i++ {
dir := filepath.Join(root, start, nextWord())
genFile(dir, 1)

if wordIdx%3 == 0 {
dir = filepath.Join(dir, nextWord())
genFile(dir, 1)
}
}
}
}

func main() {
root := "/tmp/zfind"

var c int64 = 50
interval := (int64)(endDate.Sub(startDate).Seconds()) / c
for i := range make([]int64, c) {
dateList = append(dateList, startDate.Add(time.Duration(interval*(int64)(i))*time.Second))
}

if err := os.RemoveAll(root); err == nil {
genDir(filepath.Join(root, "root"))
fmt.Println("Ready.")
} else {
fmt.Println("Failed to clean")
}
}
66 changes: 66 additions & 0 deletions scripts/run_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash
set -e

script_dir=$(dirname "$(realpath "$0")")
base_dir=$(dirname "$script_dir")
dir=$(realpath "$script_dir/../testdata/run_test")
root="/tmp/zfind/root"

if [[ ! -d $root ]]; then
echo "must run run_test_prep first"
exit 1
fi

# setup

status1=$(cd $base_dir; git status --porcelain)

$script_dir/build

rm -rf $dir
mkdir -p $dir

function zft {
local name=$1
shift
local path=$1
shift
cd $root/$path
"$base_dir/zfind" "$@" > "$dir/$name.txt"
}

# run actual tests

zft plain01 day/car

zft csv01 day/car 'type!="dir"' --csv

zft link01 / 'name like "party-%"'
zft link02 / 'name like "party-%"' -L

zft arc01 day --archive-separator=": " 'path like "life/%" and archive="zip"'

zft name01 / 'name="service-friend.md"' -l
zft name02 / 'name like "air%"' -l
zft name03 / 'name ilike "%History%" and type="dir"' .

zft ext01 / 'ext in ("jpg","jpeg") and size>100k'
zft ext02 / 'ext in ("jpg","jpeg") and size>250k and (not container or container like "%.tar.gz")' -l

zft date01 / 'date < "2002"' -l
zft date02 / 'date between "2004" and "2005-12-31"' -l

zft size01 / 'size < 1K and type="file"' -l

# check result

status2=$(cd $base_dir; git status --porcelain)
if [ -n "$status2" ]; then
if [ -n "$status1" ]; then
echo "run_tests was started with a dirty git directory, please verify the results manually."
exit 1
fi
echo "run_tests detected changes in the test output"
echo "$status2"
exit 1
fi
3 changes: 3 additions & 0 deletions scripts/tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash
set -e

script_dir=$(dirname "$(realpath "$0")")
cd $script_dir/..

go test -v ./filter
3 changes: 3 additions & 0 deletions scripts/xbuild
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash
set -eE -o pipefail

script_dir=$(dirname "$(realpath "$0")")
cd $script_dir/..

if [ -z "$version" ]; then
version=$(git rev-parse HEAD)
fi
Expand Down
11 changes: 11 additions & 0 deletions testdata/run_test/arc01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
time.zip: life/world-school.txt
time.zip: life/state-family.md
time.zip: life/student-group-country.pdf
time.zip: life/problem-hand.jpg
time.zip: life/part-place.jpeg
time.zip: life/case
time.zip: life/case/week-company.png
time.zip: life/case/system-program.mp4
time.zip: life/case/work-government-number.mp3
time.zip: life/case/night-point.csv
time.zip: life/case/home-water.txt
11 changes: 11 additions & 0 deletions testdata/run_test/csv01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name,path,container,size,date,time,ext,ext2,type,archive
city-community.mp4,city-community.mp4,,57940,2019-12-11,05:12:00,mp4,,file,
health-person-art.jpeg,face/health-person-art.jpeg,,178147,2023-06-07,11:33:36,jpeg,,file,
office-door.jpg,face/office-door.jpg,,118446,2022-12-07,10:04:48,jpg,,file,
others-level.pdf,face/others-level.pdf,,59145,2022-06-08,10:36:00,pdf,,file,
party-result.mp4,face/party-result.mp4,,299249,2024-06-05,13:31:12,mp4,,file,
war-history.png,face/war-history.png,,238648,2023-12-06,12:02:24,png,,file,
information-back.md,information-back.md,,293244,2021-12-08,08:07:12,md,,file,
kid-body.txt,kid-body.txt,,233843,2021-06-09,08:38:24,txt,,file,
name-president.mp3,name-president.mp3,,116041,2020-06-10,06:40:48,mp3,,file,
team-minute-idea.csv,team-minute-idea.csv,,174542,2020-12-09,07:09:36,csv,,file,
37 changes: 37 additions & 0 deletions testdata/run_test/date01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
2001-12-31 23:55:12 239.5K day/friend/city-community.mp4
2000-01-03 21:00:00 47.2K day/friend/father-power.pdf
2000-07-03 22:28:48 94.5K day/friend/hour-game.jpg
2001-07-03 00:26:24 190.8K day/friend/law-car.png
2001-01-01 21:57:36 142.3K day/friend/line-end-member.jpeg
2000-01-01 01:00:00 100 day/time.zip//life/world-school.txt
2000-07-01 02:28:48 601 day/time.zip//life/state-family.md
2000-12-30 01:57:36 1.5K day/time.zip//life/student-group-country.pdf
2001-06-30 04:26:24 3.1K day/time.zip//life/problem-hand.jpg
2001-12-29 03:55:12 4.9K day/time.zip//life/part-place.jpeg
2001-12-31 06:55:12 180.8K way/case/home-water.md
2001-07-02 07:26:24 143.9K way/case/night-point.txt
2000-07-03 05:28:48 71.0K way/case/system-program.mp3
2000-01-03 04:00:00 35.4K way/case/week-company.mp4
2001-01-01 04:57:36 107.1K way/case/work-government-number.csv
2000-01-04 14:00:00 58.9K way/thing.tar//change/morning-reason.mp3
2000-07-04 15:28:48 118.0K way/thing.tar//change/research-moment.csv
2001-01-02 14:57:36 177.5K way/thing.tar//change/air-teacher-force.txt
2001-07-03 17:26:24 237.7K way/thing.tar//change/education-life.md
2000-01-01 18:00:00 11.9K year/study/book-eye.png
2000-12-30 18:57:36 36.7K year/study/business-issue-side.mp3
2001-12-29 20:55:12 63.5K year/study/house-service.txt
2000-07-01 19:28:48 24.1K year/study/job-word.mp4
2001-06-30 21:26:24 50.1K year/study/kind-head.csv
2000-01-04 14:00:00 58.9K year/thing.tar.gz//change/morning-reason.mp3
2000-07-04 15:28:48 118.0K year/thing.tar.gz//change/research-moment.csv
2001-01-02 14:57:36 177.5K year/thing.tar.gz//change/air-teacher-force.txt
2001-07-03 17:26:24 237.7K year/thing.tar.gz//change/education-life.md
2000-01-04 14:00:00 58.9K year/thing.tgz//change/morning-reason.mp3
2000-07-04 15:28:48 118.0K year/thing.tgz//change/research-moment.csv
2001-01-02 14:57:36 177.5K year/thing.tgz//change/air-teacher-force.txt
2001-07-03 17:26:24 237.7K year/thing.tgz//change/education-life.md
2001-12-29 02:55:12 4.9K year/time.7z//life/part-place.jpeg
2001-06-30 02:26:24 3.1K year/time.7z//life/problem-hand.jpg
2000-07-01 00:28:48 601 year/time.7z//life/state-family.md
2000-12-30 00:57:36 1.5K year/time.7z//life/student-group-country.pdf
2000-01-01 00:00:00 100 year/time.7z//life/world-school.txt
32 changes: 32 additions & 0 deletions testdata/run_test/date02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
2004-06-29 05:19:12 245.3K day/friend/name/others-level.pdf
2004-12-28 04:48:00 49.5K day/office/door-health.jpg
2005-06-28 06:16:48 99.2K day/office/person-art.jpeg
2005-12-27 06:45:36 149.3K day/office/war-history-party.png
2004-06-26 09:19:12 10.8K day/time.zip//life/case/home-water.txt
2004-12-25 08:48:00 2.5K day/time.zip//room/mother-area.md
2005-06-25 10:16:48 5.3K day/time.zip//room/money-story.pdf
2005-12-24 10:45:36 8.5K day/time.zip//room/fact-month-lot.jpg
2004-06-28 12:19:12 186.7K way/case/room/book-eye.mp4
2005-06-27 13:16:48 75.7K way/job/issue-side.csv
2005-12-26 13:45:36 114.1K way/job/kind-head-house.txt
2004-12-27 11:48:00 37.8K way/job/word-business.mp3
2004-06-29 22:19:12 304.0K way/thing.tar//change/state/week-company.mp3
2004-12-28 21:48:00 61.3K way/thing.tar//system/program-work.csv
2005-06-28 23:16:48 122.7K way/thing.tar//system/government-number.txt
2005-12-27 23:45:36 184.5K way/thing.tar//system/night-point-home.md
2005-12-25 03:45:36 43.7K year/name/kid-body-information.csv
2005-06-26 03:16:48 28.8K year/name/minute-idea.mp3
2004-12-26 01:48:00 14.2K year/name/president-team.mp4
2004-06-27 02:19:12 69.4K year/study/friend/city-community.png
2004-06-29 22:19:12 304.0K year/thing.tar.gz//change/state/week-company.mp3
2004-12-28 21:48:00 61.3K year/thing.tar.gz//system/program-work.csv
2005-06-28 23:16:48 122.7K year/thing.tar.gz//system/government-number.txt
2005-12-27 23:45:36 184.5K year/thing.tar.gz//system/night-point-home.md
2004-06-29 22:19:12 304.0K year/thing.tgz//change/state/week-company.mp3
2004-12-28 21:48:00 61.3K year/thing.tgz//system/program-work.csv
2005-06-28 23:16:48 122.7K year/thing.tgz//system/government-number.txt
2005-12-27 23:45:36 184.5K year/thing.tgz//system/night-point-home.md
2004-06-26 07:19:12 10.8K year/time.7z//life/case/home-water.txt
2005-12-24 09:45:36 8.5K year/time.7z//room/fact-month-lot.jpg
2005-06-25 08:16:48 5.3K year/time.7z//room/money-story.pdf
2004-12-25 07:48:00 2.5K year/time.7z//room/mother-area.md
39 changes: 39 additions & 0 deletions testdata/run_test/ext01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
day/car/face/health-person-art.jpeg
day/car/face/office-door.jpg
day/friend/line-end-member.jpeg
day/group/government/area-money.jpg
day/group/government/story-fact.jpeg
day/month/head/line-end.jpeg
day/month/head/power-hour-game.jpg
day/office/research/family-student.jpg
way/case/room/fact-month-lot.jpeg
way/minute/door-health.jpg
way/point/area-money-story.jpg
way/point/fact-month.jpeg
way/teacher/country-problem.jpeg
way/teacher/student-group.jpg
way/thing.tar//body/level-office-door.jpg
way/thing.tar//body/health-person.jpeg
way/thing.tar//change/state/group-country.jpeg
way/thing.tar//issue/power-hour.jpg
way/thing.tar//issue/game-line.jpeg
way/thing.tar//life/state-family.jpg
way/thing.tar//life/student-group-country.jpeg
way/thing.tar//system/mother-area.jpg
year/head/member-law.jpeg
year/thing.tar.gz//body/level-office-door.jpg
year/thing.tar.gz//body/health-person.jpeg
year/thing.tar.gz//change/state/group-country.jpeg
year/thing.tar.gz//issue/power-hour.jpg
year/thing.tar.gz//issue/game-line.jpeg
year/thing.tar.gz//life/state-family.jpg
year/thing.tar.gz//life/student-group-country.jpeg
year/thing.tar.gz//system/mother-area.jpg
year/thing.tgz//body/level-office-door.jpg
year/thing.tgz//body/health-person.jpeg
year/thing.tgz//change/state/group-country.jpeg
year/thing.tgz//issue/power-hour.jpg
year/thing.tgz//issue/game-line.jpeg
year/thing.tgz//life/state-family.jpg
year/thing.tgz//life/student-group-country.jpeg
year/thing.tgz//system/mother-area.jpg
6 changes: 6 additions & 0 deletions testdata/run_test/ext02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2014-06-17 20:55:12 268.8K day/group/government/story-fact.jpeg
2009-06-23 13:07:12 257.0K day/office/research/family-student.jpg
2016-06-15 17:50:24 265.9K year/thing.tar.gz//body/health-person.jpeg
2011-06-22 09:02:24 256.5K year/thing.tar.gz//issue/power-hour.jpg
2011-12-21 09:31:12 321.6K year/thing.tar.gz//issue/game-line.jpeg
2006-12-27 00:43:12 309.8K year/thing.tar.gz//system/mother-area.jpg
4 changes: 4 additions & 0 deletions testdata/run_test/link01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
day/car/face/party-result.mp4
way/thing.tar//body/history/party-result.mp4
year/thing.tar.gz//body/history/party-result.mp4
year/thing.tgz//body/history/party-result.mp4
Loading

0 comments on commit e10b1c8

Please sign in to comment.