ALR

Форк
1
/
dlcache_test.go 
76 строк · 1.8 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package dlcache_test
20

21
import (
22
	"context"
23
	"crypto/sha1"
24
	"encoding/hex"
25
	"io"
26
	"os"
27
	"path/filepath"
28
	"testing"
29

30
	"plemya-x.ru/alr/internal/config"
31
	"plemya-x.ru/alr/internal/dlcache"
32
)
33

34
func init() {
35
	dir, err := os.MkdirTemp("/tmp", "alr-dlcache-test.*")
36
	if err != nil {
37
		panic(err)
38
	}
39
	config.GetPaths(context.Background()).RepoDir = dir
40
}
41

42
func TestNew(t *testing.T) {
43
	const id = "https://example.com"
44
	dir, err := dlcache.New(id)
45
	if err != nil {
46
		t.Errorf("Expected no error, got %s", err)
47
	}
48

49
	exp := filepath.Join(dlcache.BasePath(), sha1sum(id))
50
	if dir != exp {
51
		t.Errorf("Expected %s, got %s", exp, dir)
52
	}
53

54
	fi, err := os.Stat(dir)
55
	if err != nil {
56
		t.Errorf("stat: expected no error, got %s", err)
57
	}
58

59
	if !fi.IsDir() {
60
		t.Errorf("Expected cache item to be a directory")
61
	}
62

63
	dir2, ok := dlcache.Get(id)
64
	if !ok {
65
		t.Errorf("Expected Get() to return valid value")
66
	}
67
	if dir2 != dir {
68
		t.Errorf("Expected %s from Get(), got %s", dir, dir2)
69
	}
70
}
71

72
func sha1sum(id string) string {
73
	h := sha1.New()
74
	_, _ = io.WriteString(h, id)
75
	return hex.EncodeToString(h.Sum(nil))
76
}
77

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.