Skip to content

Commit

Permalink
also use comma as input separator and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
elkemper committed Mar 3, 2024
1 parent 1319632 commit 4d37d9f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# test-skipper (tssk) - Manage skipped tests in the repository


Using commands 'skip' or 'unskip' you can easily modify the 'skipped.cfg' file that will contain your test id's in plain text comma separated format.

### Usage:
tssk skip <test_ids>
Expand All @@ -12,6 +12,7 @@ unskip - Remove tests from the `skipped.cfg` file

### Examples:
tssk skip 123 2453
tssk skip 123, 234
tssk unskip 123

## Build
Expand Down
78 changes: 55 additions & 23 deletions cmd/tssk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ import (
"strings"
)

const skippedFileName = "skipped.cfg"
const delimiter = ", "
const SkippedFileName = "skipped.cfg"
const Comma = ","
const Separator = Comma

type Configuration struct {
FilePath string
Format FileFormat
}

type Command uint8
type FileFormat uint8
Expand Down Expand Up @@ -41,6 +47,8 @@ func (command Command) Name() string {
}

func main() {
configuration := GetConfiguration()
skippedFileName, fileFormat := configuration.FilePath, configuration.Format
flag.Parse()
args := flag.Args()
if len(args) > 0 {
Expand All @@ -53,7 +61,7 @@ func main() {
if !slices.Contains(possible, command) {
log.Fatalf("Provided command isn't possible.")
}
err = PerformAction(command, skippedFileName, testCodes)
err = PerformAction(skippedFileName, fileFormat, command, testCodes)
if err != nil {
log.Fatalf("Error:%s", err)
return
Expand All @@ -64,10 +72,14 @@ func main() {
}
}

func parseArgs(args []string) (Command, []string, error) {
func GetConfiguration() Configuration {
return Configuration{SkippedFileName, PlainText}
}

func parseArgs(args []string) (Command, []TestCode, error) {
commandString := args[0]
command, err := parseCommand(commandString)
testCodes := args[1:]
command, err := parseCommandArg(commandString)
testCodes := parseTestCodesArgs(args[1:])
if err != nil {
return command, testCodes, err
}
Expand All @@ -85,21 +97,33 @@ func possibleCommands(skippedFileName string) []Command {
return []Command{Skip}
}

func parseTestCodesArgs(testCodeStrings []string) []TestCode {
testCodes := make([]TestCode, 0)
for _, stringCode := range testCodeStrings {
commaSeparatedParts := strings.Split(stringCode, Separator)
for _, part := range commaSeparatedParts {
part = strings.TrimSpace(part)
testCodes = append(testCodes, TestCode(part))
}
}
return testCodes
}

// PerformAction is the base function that receives all the arguments
// and decides what to do.
func PerformAction(command Command, skippedFileName string, testCodes []string) error {
func PerformAction(skippedFileName string, fileFormat FileFormat, command Command, testCodes []TestCode) error {
var skippedTests SkippedTests
testSet := make(TestSet, 0)
skippedTests.Tests = testSet
var err error
if !bypassFileReading(skippedFileName, command) {
skippedTests, err = readSkippedTests(skippedFileName)
skippedTests, err = readSkippedTests(skippedFileName, fileFormat)
if err != nil {
return errors.New("error occurred in reading the file")
}
}

var notProcessed []string
var notProcessed []TestCode
var notProcessingReason string
if command == Skip {
notProcessed = skippedTests.Tests.add(testCodes)
Expand All @@ -112,14 +136,14 @@ func PerformAction(command Command, skippedFileName string, testCodes []string)
fmt.Printf("Couldn't %s: %s. Reason: %s\n", command.Name(), notProcessed, notProcessingReason)
}
fmt.Printf("Saving %d code(s): %s\n", len(skippedTests.Tests), skippedTests.Tests.toString())
err = saveSkippedTests(skippedFileName, PlainText, skippedTests)
err = saveSkippedTests(skippedFileName, fileFormat, skippedTests)
if err != nil {
return errors.New("error occurred in writing to file")
}
return nil
}

func parseCommand(commandString string) (Command, error) {
func parseCommandArg(commandString string) (Command, error) {
var command Command
switch commandString {
case "skip":
Expand All @@ -142,34 +166,42 @@ func bypassFileReading(skippedFileName string, command Command) bool {
return !fileExists && command == Skip
}

func saveSkippedTests(fileName string, format FileFormat, skippedTests SkippedTests) error {
func saveSkippedTests(fileName string, fileFormat FileFormat, skippedTests SkippedTests) error {
var testCodesSingleString string
if format == PlainText {
testCodesSingleString = strings.Join(skippedTests.Tests.toString(), delimiter)
if fileFormat == PlainText {
const prettySeparator = Comma + " "
testCodesSingleString = strings.Join(skippedTests.Tests.toString(), prettySeparator)
} else {
return errors.New("only plain text format is supported")
}
err := os.WriteFile(skippedFileName, []byte(testCodesSingleString), 0644)

err := os.WriteFile(SkippedFileName, []byte(testCodesSingleString), 0644)
if err != nil {
return err
}
return nil
}

func readSkippedTests(fileName string) (SkippedTests, error) {
func readSkippedTests(fileName string, fileFormat FileFormat) (SkippedTests, error) {
var skippedTests SkippedTests
contents, err := os.ReadFile(skippedFileName)
var contents []byte
var testSet TestSet
contents, err := os.ReadFile(SkippedFileName)
if err != nil {
return skippedTests, errors.New("error occurred in reading the file")
}

contentString := string(contents)
var testSlice []string
if len(strings.TrimSpace(contentString)) > 0 {
testSlice = strings.Split(contentString, delimiter)
if fileFormat == PlainText {
contentString := string(contents)
var testCodeStrings []string
if len(strings.TrimSpace(contentString)) > 0 {
testCodeStrings = strings.Split(contentString, Separator)
}
testSet = makeTestSet(testCodeStrings)
} else {
return skippedTests, errors.New("only plain text format is supported")
}
testSet := makeTestSet(testSlice)

skippedTests.Tests = testSet
return skippedTests, nil
}
36 changes: 22 additions & 14 deletions cmd/tssk/test_set.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

import (
"strings"
)

type TestSet map[TestCode]struct{}

func (testSet TestSet) toString() []string {
Expand All @@ -12,37 +16,41 @@ func (testSet TestSet) toString() []string {
return testCodes
}

func (testSet TestSet) add(testCodes []string) []string {
var notProcessed []string
for _, stringCode := range testCodes {
testCode := TestCode(stringCode)
func (testSet TestSet) add(testCodes []TestCode) []TestCode {
var notProcessed []TestCode
for _, testCode := range testCodes {
if len(testCode) <= 0 {
continue
}
if _, exists := testSet[testCode]; !exists {
testSet[testCode] = struct{}{}
} else {
notProcessed = append(notProcessed, stringCode)
notProcessed = append(notProcessed, testCode)
}
}
return notProcessed
}

func (testSet TestSet) remove(testCodes []string) []string {
var notProcessed []string
for _, stringCode := range testCodes {
testCode := TestCode(stringCode)
func (testSet TestSet) remove(testCodes []TestCode) []TestCode {
var notProcessed []TestCode
for _, testCode := range testCodes {
if _, exists := testSet[testCode]; exists {
delete(testSet, testCode)
} else {
notProcessed = append(notProcessed, stringCode)
notProcessed = append(notProcessed, testCode)
}
}
return notProcessed
}

func makeTestSet(testCodes []string) TestSet {
testSet := make(TestSet, len(testCodes))
for ind := range testCodes {
testCode := TestCode(testCodes[ind])
testSet[testCode] = struct{}{}
testSet := make(TestSet, 0)
for _, testCodeString := range testCodes {
testCodeString = strings.TrimSpace(testCodeString)
if len(testCodeString) > 0 {
testCode := TestCode(testCodeString)
testSet[testCode] = struct{}{}
}
}
return testSet
}

0 comments on commit 4d37d9f

Please sign in to comment.