Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support list_files, mkdir and remove_files_wildcard #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions r/example.result
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ a b
SELECT 1 FROM NON_EXISTING_TABLE;
Error 1146 (42S02): Table 'example.NON_EXISTING_TABLE' doesn't exist
SELECT 2 FROM NON_EXISTING_TABLE;
Error 1146 (42S02): Table 'example.NON_EXISTING_TABLE' doesn't exist
SELECT 3 FROM NON_EXISTING_TABLE;
Got one of the listed errors
Error 1146 (42S02): Table 'example.NON_EXISTING_TABLE' doesn't exist
SELECT 4;
4
4
Expand All @@ -20,8 +21,9 @@ SELECT 6;
1 SELECT;
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 1 near "1 SELECT;"
2 SELECT;
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 1 near "2 SELECT;"
3 SELECT;
Got one of the listed errors
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 1 near "3 SELECT;"
explain analyze format='brief' select * from t;
id estRows actRows task access object execution info operator info memory disk
TableReader 10000.00 5 root NULL time:<num>, loops:<num>, RU:<num>, cop_task: {num:<num>, max:<num>, proc_keys:<num>, rpc_num:<num>, rpc_time:<num>, copr_cache_hit_ratio:<num>, build_task_duration:<num>, max_distsql_concurrency:<num>} data:TableFullScan <num> Bytes N/A
Expand All @@ -33,6 +35,27 @@ TableReader_5 10000.00 5 root NULL time:<num>, loops:<num>, RU:<num>, cop_task:
insert into t values (6, 6);
affected rows: 1
info:
--list_files ./src
main.go
perror.go
query.go
query_test.go
type.go
util.go
util_test.go
xml.go
--list_files ./src *.go
main.go
perror.go
query.go
query_test.go
type.go
util.go
util_test.go
xml.go
--list_files ./src *_test.go
query_test.go
util_test.go
DROP TABLE IF EXISTS t1;
affected rows: 0
info:
Expand All @@ -47,3 +70,5 @@ affected rows: 3
info: Records: 2 Duplicates: 1 Warnings: 0
1
use `test`;;
0
0
2 changes: 1 addition & 1 deletion r/extensions.result
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT 1 FROM NON_EXISTING_TABLE;
Got one of the listed errors
Error 1146 (42S02): Table 'extensions.NON_EXISTING_TABLE' doesn't exist
79 changes: 79 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"database/sql"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -493,6 +494,53 @@ func (t *tester) Run() error {
return errors.Annotate(err, fmt.Sprintf("Could not parse regex in --replace_regex: line: %d sql:%v", q.Line, q.Query))
}
t.replaceRegex = regex
case Q_MKDIR:
q.Query = strings.TrimSpace(q.Query)
if err := os.MkdirAll(q.Query, os.ModePerm); err != nil {
t.buf.WriteString("1\n")
return errors.Annotate(err, fmt.Sprintf("failed to create directories: %s", q.Query))
}
t.buf.WriteString("0\n")
case Q_LIST_FILES:
q.Query = strings.TrimSpace(q.Query)
parts := strings.Split(q.Query, " ")
if len(parts) < 1 {
return errors.New("list_files contains at least 1 argument")
}
if len(parts) > 2 {
return errors.New("list_files contains at most 2 arguments")
}

files, err := getFilesInDir(parts[0])
if err != nil {
return err
}

if len(parts) == 2 {
files = filterByPattern(files, parts[1])
}
for _, file := range files {
t.buf.WriteString(file.Name())
t.buf.WriteString("\n")
}
case Q_REMOVE_FILES_WILDCARD:
q.Query = strings.TrimSpace(q.Query)
parts := strings.Split(q.Query, " ")
if len(parts) != 2 {
return errors.New("remove_files_wildcard requires 2 arguments")
}

files, err := getFilesInDir(parts[0])
if err != nil {
return err
}
files = filterByPattern(files, parts[1])
for _, file := range files {
err = os.Remove(filepath.Join(parts[0], file.Name()))
if err != nil {
continue
}
}
default:
log.WithFields(log.Fields{"command": q.firstWord, "arguments": q.Query, "line": q.Line}).Warn("command not implemented")
}
Expand All @@ -507,6 +555,37 @@ func (t *tester) Run() error {
return t.flushResult()
}

func getFilesInDir(dirPath string) ([]fs.DirEntry, error) {
fileInfo, err := os.Stat(dirPath)
if os.IsNotExist(err) {
return nil, errors.Annotate(err, fmt.Sprintf("Directory %s not exists", dirPath))
}
if err != nil {
return nil, errors.Annotate(err, fmt.Sprintf("Fail to get infomation for directory %s", dirPath))
}
if !fileInfo.IsDir() {
return nil, errors.Annotate(err, fmt.Sprintf("%s is not a directory", dirPath))
}
files, err := os.ReadDir(dirPath)
if err != nil {
return nil, errors.Annotate(err, fmt.Sprintf("Fail to list files insides %s", dirPath))
}
return files, nil
}

func filterByPattern(files []fs.DirEntry, pattern string) []fs.DirEntry {
targetFiles := []fs.DirEntry{}
for _, file := range files {
name := file.Name()
match, err := filepath.Match(pattern, name)
if err != nil || !match {
continue
}
targetFiles = append(targetFiles, file)
}
return targetFiles
}

func (t *tester) concurrentRun(concurrentQueue []query, concurrentSize int) error {
if len(concurrentQueue) == 0 {
return nil
Expand Down
12 changes: 12 additions & 0 deletions t/example.test
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ explain analyze select * from t;
--enable_info
insert into t values (6, 6);

--echo --list_files ./src
--list_files ./src
--echo --list_files ./src *.go
--list_files ./src *.go
--echo --list_files ./src *_test.go
--list_files ./src *_test.go

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (f1 INT PRIMARY KEY, f2 INT NOT NULL UNIQUE);
INSERT t1 VALUES (1, 1);
Expand All @@ -48,3 +55,8 @@ INSERT t1 VALUES (1, 1), (1, 1) ON DUPLICATE KEY UPDATE f1 = 2, f2 = 2;
--echo $a

use `test`;;

--mkdir ./tmp
--mkdir ./tmp1
--remove_files_wildcard ./tmp *
--remove_files_wildcard ./tmp1 a
Loading