1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-27 23:18:34 +02:00

сделал Show_Version()

This commit is contained in:
Nikitin Aleksandr
2024-10-14 14:25:20 +03:00
parent 00cee818f7
commit 8d72ad6398
2 changed files with 221 additions and 0 deletions

View File

@@ -1035,3 +1035,120 @@ func DeleteEndEndline(Text string) string {
return Otvet return Otvet
} }
// Find_Directory_ModifiedTime - возвращает дату последнего изменения в папке internal
func Find_Directory_ModifiedTime(FolderName string) (time.Time, error) {
var Otvet time.Time
var err error
dir := ProgramDir()
dir = dir + FolderName
ok, err := FileExists(dir)
if err != nil {
err = fmt.Errorf("Find_Directory_ModifiedTime() FileExists() error: %w", err)
return Otvet, err
}
if ok == false {
err = fmt.Errorf("Find_Directory_ModifiedTime() FileExists() error: file not exists: %s", dir)
return Otvet, err
}
//найдём дату папки
f, err := os.Open(dir)
if err != nil {
err = fmt.Errorf("Find_Directory_ModifiedTime() os.Open() error: %w", err)
return Otvet, err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
err = fmt.Errorf("Find_Directory_ModifiedTime() f.Stat() error: %w", err)
return Otvet, err
}
Otvet = stat.ModTime()
return Otvet, err
}
// Show_Repository_Code_ModifiedTime - выводит дату последнего изменения в папках cmd, internal, pkg, vendor
func Show_Repository_Code_ModifiedTime() {
Date, err := Find_Repository_Code_ModifiedTime()
if err != nil {
println("Find_Repository_Code_ModifiedTime() error: ", err.Error())
return
}
if Date.IsZero() {
println("Last repository code modified time: not found")
return
}
println("Last repository code modified time: ", Date.String())
}
// Find_Repository_Code_ModifiedTime - возвращает дату последнего изменения в папках cmd, internal, pkg, vendor
func Find_Repository_Code_ModifiedTime() (time.Time, error) {
var Otvet time.Time
var err error
//cmd
Time_cmd, err := Find_Directory_ModifiedTime("cmd")
if err != nil {
//return Otvet, err
}
//internal
Time_internal, err := Find_Directory_ModifiedTime("internal")
if err != nil {
//return Otvet, err
}
//pkg
Time_pkg, err := Find_Directory_ModifiedTime("pkg")
if err != nil {
//return Otvet, err
}
//vendor
Time_vendor, err := Find_Directory_ModifiedTime("vendor")
if err != nil {
//return Otvet, err
}
//выбираем максимальную дату
Otvet = TimeMax(Time_cmd, Time_internal, Time_pkg, Time_vendor)
return Otvet, err
}
// TimeMax - возвращает максимальную дату
func TimeMax(x time.Time, y ...time.Time) time.Time {
maxTime := x
for _, val := range y {
if val.After(maxTime) {
maxTime = val
}
}
return maxTime
}
// TimeMin - возвращает минимальную дату
func TimeMin(x time.Time, y ...time.Time) time.Time {
minTime := x
for _, val := range y {
if val.Before(minTime) {
minTime = val
}
}
return minTime
}
// Show_Version - выводит версию сервиса на экран
func Show_Version(Version string) {
println("Service version: ", Version)
}

View File

@@ -1046,3 +1046,107 @@ func TestDeleteEndEndline(t *testing.T) {
}) })
} }
} }
func TestFind_ModifiedTime_Internal(t *testing.T) {
_, err := Find_Directory_ModifiedTime("vendor")
if err != nil {
t.Error(err)
}
}
func TestTimeMax(t *testing.T) {
// Testing when the first date is the maximum
x := time.Date(2023, 03, 13, 0, 0, 0, 0, time.UTC)
y1 := time.Date(2023, 03, 10, 0, 0, 0, 0, time.UTC)
y2 := time.Date(2023, 03, 11, 0, 0, 0, 0, time.UTC)
result := TimeMax(x, y1, y2)
if result != x {
t.Error("Expected first date as the maximum, but got: ", result)
}
// Testing when the last date is the maximum
x = time.Date(2023, 03, 10, 0, 0, 0, 0, time.UTC)
y1 = time.Date(2023, 03, 11, 0, 0, 0, 0, time.UTC)
y2 = time.Date(2023, 03, 12, 0, 0, 0, 0, time.UTC)
result = TimeMax(x, y1, y2)
if result != y2 {
t.Error("Expected last date as the maximum, but got: ", result)
}
// Testing when the middle date is the maximum
x = time.Date(2023, 03, 10, 0, 0, 0, 0, time.UTC)
y1 = time.Date(2023, 03, 12, 0, 0, 0, 0, time.UTC)
y2 = time.Date(2023, 03, 11, 0, 0, 0, 0, time.UTC)
result = TimeMax(x, y1, y2)
if result != y1 {
t.Error("Expected middle date as the maximum, but got: ", result)
}
// Testing when all dates are the same
x = time.Date(2023, 03, 10, 0, 0, 0, 0, time.UTC)
y1 = time.Date(2023, 03, 10, 0, 0, 0, 0, time.UTC)
y2 = time.Date(2023, 03, 10, 0, 0, 0, 0, time.UTC)
result = TimeMax(x, y1, y2)
if result != x {
t.Error("Expected all dates the same, should return the first date, but got: ", result)
}
}
func TestTimeMin(t *testing.T) {
// Case 1: Test when x is the only time provided
x := time.Date(2023, 3, 14, 12, 0, 0, 0, time.UTC)
result := TimeMin(x)
if result != x {
t.Errorf("Expected %v, but got %v", x, result)
}
// Case 2: Test when y is the only time provided
y := time.Date(2023, 3, 15, 12, 0, 0, 0, time.UTC)
result = TimeMin(y)
if result != y {
t.Errorf("Expected %v, but got %v", y, result)
}
// Case 3: Test when multiple times are provided and the minimum is in the middle
x = time.Date(2023, 3, 14, 12, 0, 0, 0, time.UTC)
y = time.Date(2023, 3, 15, 12, 0, 0, 0, time.UTC)
z := time.Date(2023, 3, 13, 12, 0, 0, 0, time.UTC)
result = TimeMin(x, y, z)
if result != z {
t.Errorf("Expected %v, but got %v", z, result)
}
// Case 4: Test when the minimum time is the first one provided
x = time.Date(2023, 3, 13, 12, 0, 0, 0, time.UTC)
y = time.Date(2023, 3, 15, 12, 0, 0, 0, time.UTC)
z = time.Date(2023, 3, 14, 12, 0, 0, 0, time.UTC)
result = TimeMin(x, y, z)
if result != x {
t.Errorf("Expected %v, but got %v", x, result)
}
// Case 5: Test when the minimum time is the last one provided
x = time.Date(2023, 3, 15, 12, 0, 0, 0, time.UTC)
y = time.Date(2023, 3, 14, 12, 0, 0, 0, time.UTC)
z = time.Date(2023, 3, 13, 12, 0, 0, 0, time.UTC)
result = TimeMin(x, y, z)
if result != z {
t.Errorf("Expected %v, but got %v", z, result)
}
}
func TestFind_Repository_Code_ModifiedTime(t *testing.T) {
_, err := Find_Repository_Code_ModifiedTime()
if err != nil {
t.Error(err)
}
}
func TestShow_Repository_Code_ModifiedTime(t *testing.T) {
Show_Repository_Code_ModifiedTime()
}
func TestShow_Version(t *testing.T) {
Show_Version("1")
}