Added test cases for Delete (#8)

* Completed List cmd and added API calls

* Minor comments and add delete code to pass linting

* Typo in descriptions

* Minor comments

* Validations

* Validations-1

* improved branch flag validation

* removed build

* working after refactory with bad names

* Command working, test not working

* Corrected creation of service

* Finalized structure using service

* Deleted tests

* cleanup

* cleanup

* cleanup

* removed space with tab

* aligned types in model.go

* Update model.go

* resolved comments

* Refactor

* removed long descriptions

* Working incomplete tests

* Completed tests

* cleanup

* checks

* PR comments

* PR comments

* minor comment issue

* minor comment issue

* Added test cases for Delete

* updated tests to work with workflow

* Added missing error condition

* Updated tests to support new option service

* Improved eror handling for list

* Fixed test case

* Improved error handling

* Error handling and test cases for delete API calls

* Added test case for user confirmation delete.

* Removed unused import from test

* Fixed test case for error scenario

* Upgraded go-gh

* reusing rest client error

* Fix for failing windows test cases

* help cmd removed when cache isnt present on delete

* Pretty print ratio and space between cols modified

* Error handling wrapping

* Reverted back error message after silencing help

Co-authored-by: t-dedah <t-dedah@github.com>
Co-authored-by: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com>
This commit is contained in:
Sankalp Kotewar
2022-07-14 02:33:36 +05:30
committed by GitHub
parent db34270ecb
commit 790f19d8a6
5 changed files with 237 additions and 49 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"math"
"os"
"os/exec"
"strconv"
"strings"
"unicode/utf8"
@@ -11,8 +13,10 @@ import (
"github.com/actions/gh-actions-cache/types"
gh "github.com/cli/go-gh"
ghRepo "github.com/cli/go-gh/pkg/repository"
"github.com/moby/term"
"github.com/cli/safeexec"
"github.com/mattn/go-isatty"
"github.com/nleeper/goment"
"golang.org/x/term"
)
const MB_IN_BYTES = 1024 * 1024
@@ -45,14 +49,13 @@ func FormatCacheSize(size_in_bytes float64) string {
}
func PrettyPrintCacheList(caches []types.ActionsCache) {
fd := os.Stdout.Fd()
ws, _ := term.GetWinsize(fd)
width := math.Max(math.Min(float64(ws.Width), 180), 100)
width, _, _ := getTerminalWidth(os.Stdout)
width = int(math.Max(float64(width), 100))
sizeWidth := SIZE_COLUMN_WIDTH // hard-coded size as the content is scoped
timeWidth := LAST_ACCESSED_AT_COLUMN_WIDTH // hard-coded size as the content is scoped
keyWidth := int(math.Floor(0.75 * (width - 15 - 20)))
refWidth := int(math.Floor(0.25 * (width - 15 - 20)))
keyWidth := int(math.Floor(0.65 * float64(width-15-20)))
refWidth := int(math.Floor(0.20 * float64(width-15-20)))
for _, cache := range caches {
var formattedRow string = getFormattedCacheInfo(cache, keyWidth, sizeWidth, refWidth, timeWidth)
@@ -90,7 +93,7 @@ func getFormattedCacheInfo(cache types.ActionsCache, keyWidth int, sizeWidth int
size := trimOrPad(fmt.Sprintf("[%s]", FormatCacheSize(cache.SizeInBytes)), sizeWidth)
ref := trimOrPad(cache.Ref, refWidth)
time := trimOrPad(lastAccessedTime(cache.LastAccessedAt), timeWidth)
return fmt.Sprintf("%s %s %s %s", key, size, ref, time)
return fmt.Sprintf("%s %s %s %s", key, size, ref, time)
}
func RedTick() string {
@@ -106,3 +109,21 @@ func PrintSingularOrPlural(count int, singularStr string, pluralStr string) stri
}
return fmt.Sprintf("%d %s", count, pluralStr)
}
func getTerminalWidth(f *os.File) (int, int, error) {
if !isatty.IsCygwinTerminal(f.Fd()) {
return term.GetSize(int(f.Fd()))
}
tputExe, err := safeexec.LookPath("tput")
if err != nil {
return 100, 100, nil
}
tputCmd := exec.Command(tputExe, "cols")
tputCmd.Stdin = os.Stdin
if out, err := tputCmd.Output(); err == nil {
if w, err := strconv.Atoi(strings.TrimSpace(string(out))); err == nil {
return w, 100, nil
}
}
return 100, 100, nil
}