mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-25 23:02:22 +02:00
сделал Find_LastTagVersion()
This commit is contained in:
74
git/git.go
Normal file
74
git/git.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Find_LastTagVersion - возвращает последнюю версию в гит, образцы:
|
||||
// v1.2.159-15-ga4b0c32b
|
||||
// v1.2.159-14-gafa2f9b5
|
||||
// v1.2.159-13-g27f8c242
|
||||
// v1.2.159-12-gc716a327
|
||||
// v1.2.159-11-g1c7efca0
|
||||
// v1.2.159-10-g1f369547
|
||||
// v1.2.159-9-gdc9f7202
|
||||
// v1.2.159-8-g6c40f58b
|
||||
// v1.2.159-7-g1052bb20
|
||||
// v1.2.159-6-ged68de47
|
||||
// v1.2.159-5-gaf92f802
|
||||
// v1.2.159-4-gb49931d5
|
||||
// v1.2.159-3-g0ff81ea4
|
||||
// v1.2.159-2-gc29d509e
|
||||
// v1.2.159-1-g3d8ae0fd
|
||||
// v1.2.159
|
||||
// v1.2.158-20-g06257859
|
||||
// v1.2.158-15-g1cbe3bc2
|
||||
// v1.2.158-14-gcf2bce22
|
||||
// v1.2.158-13-gc45d16a8
|
||||
func Find_LastTagVersion() (string, error) {
|
||||
Otvet := ""
|
||||
var err error
|
||||
|
||||
//найдём список Хэшей коммитов
|
||||
cmd := "git"
|
||||
arg := make([]string, 0)
|
||||
arg = append(arg, "rev-list")
|
||||
arg = append(arg, "--all")
|
||||
arg = append(arg, "--max-count=1")
|
||||
|
||||
output, err := micro.ExecuteShellCommand(cmd, arg...)
|
||||
if err != nil {
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
return Otvet, err
|
||||
}
|
||||
MassHash0 := strings.Split(output, "\n")
|
||||
MassHash := make([]string, 0)
|
||||
|
||||
for _, v := range MassHash0 {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
MassHash = append(MassHash, v)
|
||||
}
|
||||
|
||||
//найдём версии их Хэшей
|
||||
cmd = "git"
|
||||
arg = make([]string, 0)
|
||||
arg = append(arg, "describe")
|
||||
arg = append(arg, "--always")
|
||||
arg = append(arg, "--tags")
|
||||
arg = append(arg, MassHash...)
|
||||
|
||||
Otvet, err = micro.ExecuteShellCommand(cmd, arg...)
|
||||
if err != nil {
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
Otvet = micro.DeleteEndEndline(Otvet)
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
15
git/git_test.go
Normal file
15
git/git_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package git
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFind_LastTagVersion(t *testing.T) {
|
||||
|
||||
Otvet, err := Find_LastTagVersion()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if Otvet == "" {
|
||||
t.Error("TestFind_LastTagVersion() error: Otvet =''")
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"hash/fnv"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sort"
|
||||
@@ -1004,3 +1005,33 @@ func Int32FromString(s string) (int32, error) {
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
// ExecuteShellCommand - выполняет команду в shell, и возвращает строку результата
|
||||
func ExecuteShellCommand(TextCommand string, args ...string) (string, error) {
|
||||
Otvet := ""
|
||||
var err error
|
||||
|
||||
MassByte, err := exec.Command(TextCommand, args...).CombinedOutput()
|
||||
Otvet = string(MassByte)
|
||||
if err != nil {
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
// DeleteEndEndline - убирает в конце "\n"
|
||||
func DeleteEndEndline(Text string) string {
|
||||
Otvet := Text
|
||||
|
||||
if Otvet == "" {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
LastSymbol := Otvet[len(Otvet)-1:]
|
||||
if LastSymbol == "\n" {
|
||||
Otvet = Otvet[0 : len(Otvet)-1]
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
@@ -935,3 +935,114 @@ func TestPause_ctx(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestExecuteShellCommand_EmptyCommand(t *testing.T) {
|
||||
expected := ""
|
||||
result, err := ExecuteShellCommand("")
|
||||
if result != expected || err == nil {
|
||||
t.Errorf("Expected empty result and non-nil error, but got result: %s, error: %v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteShellCommand_ValidCommand(t *testing.T) {
|
||||
expectedOutput := "Hello, World!"
|
||||
cmd := "echo"
|
||||
arg := "Hello, World!"
|
||||
|
||||
output, err := ExecuteShellCommand(cmd, arg)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if output != (expectedOutput + "\n") {
|
||||
t.Errorf("Expected: %s, but got: %s", expectedOutput, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteShellCommand_InvalidCommand(t *testing.T) {
|
||||
cmd := "invalidcommand"
|
||||
|
||||
_, err := ExecuteShellCommand(cmd)
|
||||
if err == nil {
|
||||
t.Error("Expected an error for invalid command, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteShellCommand_Git(t *testing.T) {
|
||||
//найдём список Хэшей коммитов
|
||||
cmd := "git"
|
||||
arg := make([]string, 0)
|
||||
arg = append(arg, "rev-list")
|
||||
arg = append(arg, "--all")
|
||||
arg = append(arg, "--max-count=1")
|
||||
|
||||
output, err := ExecuteShellCommand(cmd, arg...)
|
||||
if err != nil {
|
||||
t.Errorf("TestExecuteShellCommand_Git() error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
t.Error("TestExecuteShellCommand_Git() error: Output=''")
|
||||
return
|
||||
}
|
||||
MassHash0 := strings.Split(output, "\n")
|
||||
MassHash := make([]string, 0)
|
||||
|
||||
for _, v := range MassHash0 {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
MassHash = append(MassHash, v)
|
||||
}
|
||||
|
||||
//найдём версии их Хэшей
|
||||
cmd = "git"
|
||||
arg = make([]string, 0)
|
||||
arg = append(arg, "describe")
|
||||
arg = append(arg, "--always")
|
||||
arg = append(arg, "--tags")
|
||||
arg = append(arg, MassHash...)
|
||||
|
||||
output, err = ExecuteShellCommand(cmd, arg...)
|
||||
if err != nil {
|
||||
t.Errorf("TestExecuteShellCommand_Git() error: %v", err)
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
t.Error("TestExecuteShellCommand_Git() error: Output=''")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteEndEndline(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "Text ends with \\n",
|
||||
input: "example\n",
|
||||
expected: "example",
|
||||
},
|
||||
{
|
||||
name: "Text does not end with \\n",
|
||||
input: "example",
|
||||
expected: "example",
|
||||
},
|
||||
{
|
||||
name: "Empty string",
|
||||
input: "",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result := DeleteEndEndline(test.input)
|
||||
if result != test.expected {
|
||||
t.Errorf("Expected %s, but got %s", test.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user