-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iter20: Указание версии приложения при сборке и вывод этой версии при старте приложения. #21
Changes from all commits
2e12655
548f32b
a8d4139
a25980e
4af5cfd
3873aad
c73094f
8f8f1d7
30b8984
2fa7511
d1b7aa1
484b862
5e039b2
1e98761
e7822e9
ccaf5ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: coverage-report | ||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
coveragereport: | ||
runs-on: ubuntu-latest | ||
|
||
services: | ||
postgresql: | ||
image: postgres | ||
env: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: test | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 5s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Test | ||
env: | ||
TEST_DSN: postgres://postgres:postgres@localhost:5432/test?sslmode=disable | ||
run: go test -coverprofile ./c.out $(go list ./... | grep -v /mock/) | ||
|
||
- name: Update coverage report | ||
uses: ncruces/go-coverage-report@v0 | ||
with: | ||
coverage-file: ./c.out | ||
report: true | ||
chart: true | ||
amend: true | ||
continue-on-error: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/kosalnik/metrics/internal/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_parseFlags(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Группируй связанные тестовые случаи в подтесты с помощью t.Run для улучшения структуры и читаемости тестов |
||
cases := []struct { | ||
name string | ||
flags []string | ||
env map[string]string | ||
want func(c *config.Agent) | ||
}{ | ||
{ | ||
name: "CollectorAddress in flag", | ||
flags: []string{"script", "-a=example.com:123"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, "example.com:123", c.CollectorAddress) | ||
}, | ||
}, | ||
{ | ||
name: "CollectorAddress in ENV", | ||
flags: []string{"script"}, | ||
env: map[string]string{"ADDRESS": "example.com:123"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, "example.com:123", c.CollectorAddress) | ||
}, | ||
}, | ||
{ | ||
name: "CollectorAddress in flags and ENV", | ||
flags: []string{"script", "-a=example.com:123"}, | ||
env: map[string]string{"ADDRESS": "example.com:456"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, "example.com:456", c.CollectorAddress) | ||
}, | ||
}, | ||
{ | ||
name: "REPORT_INTERVAL in flag", | ||
flags: []string{"script", "-r=33"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(33), c.ReportInterval) | ||
}, | ||
}, | ||
{ | ||
name: "REPORT_INTERVAL in ENV", | ||
flags: []string{"script"}, | ||
env: map[string]string{"REPORT_INTERVAL": "11"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(11), c.ReportInterval) | ||
}, | ||
}, | ||
{ | ||
name: "REPORT_INTERVAL in flags and ENV", | ||
flags: []string{"script", "-r=33"}, | ||
env: map[string]string{"REPORT_INTERVAL": "11"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(11), c.ReportInterval) | ||
}, | ||
}, | ||
{ | ||
name: "RATE_LIMIT in flag", | ||
flags: []string{"script", "-l=13"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(13), c.RateLimit) | ||
}, | ||
}, | ||
{ | ||
name: "RATE_LIMIT in ENV", | ||
flags: []string{"script"}, | ||
env: map[string]string{"RATE_LIMIT": "34"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(34), c.RateLimit) | ||
}, | ||
}, | ||
{ | ||
name: "RATE_LIMIT in flags and ENV", | ||
flags: []string{"script", "-l=13"}, | ||
env: map[string]string{"RATE_LIMIT": "55"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(55), c.RateLimit) | ||
}, | ||
}, | ||
{ | ||
name: "PROFILING in ENV", | ||
flags: []string{"script"}, | ||
env: map[string]string{"PROFILING": "true"}, | ||
want: func(c *config.Agent) { | ||
assert.True(t, c.Profiling.Enabled) | ||
}, | ||
}, | ||
{ | ||
name: "POLL_INTERVAL in ENV", | ||
flags: []string{"script"}, | ||
env: map[string]string{"POLL_INTERVAL": "123"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, int64(123), c.PollInterval) | ||
}, | ||
}, | ||
{ | ||
name: "KEY false in ENV", | ||
flags: []string{"script"}, | ||
env: map[string]string{"KEY": "jasdjhfqwehriuh"}, | ||
want: func(c *config.Agent) { | ||
assert.Equal(t, "jasdjhfqwehriuh", c.Hash.Key) | ||
}, | ||
}, | ||
} | ||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := config.NewAgent() | ||
if tt.env == nil { | ||
parseFlags(tt.flags, c) | ||
} else { | ||
old := make(map[string]string, len(tt.env)) | ||
for k, v := range tt.env { | ||
old[k] = os.Getenv(k) | ||
require.NoError(t, os.Setenv(k, v)) | ||
} | ||
parseFlags(tt.flags, c) | ||
for k, v := range old { | ||
require.NoError(t, os.Setenv(k, v)) | ||
} | ||
} | ||
if tt.want != nil { | ||
tt.want(c) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,18 @@ import ( | |
"github.com/kosalnik/metrics/internal/config" | ||
) | ||
|
||
func parseFlags(c *config.Server) { | ||
flag.StringVar(&c.Address, "a", ":8080", "server endpoint (ip:port)") | ||
flag.IntVar(&c.Backup.StoreInterval, "i", 300, "Store interval") | ||
flag.StringVar(&c.Backup.FileStoragePath, "f", "/tmp/metrics-db.json", "File storage path") | ||
flag.BoolVar(&c.Backup.Restore, "r", true, "Restore storage before start") | ||
flag.StringVar(&c.DB.DSN, "d", "", "Database DSN") | ||
flag.StringVar(&c.Hash.Key, "k", "", "SHA256 Key") | ||
flag.Parse() | ||
func parseFlags(args []string, c *config.Server) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Создай отдельный пакет для обработки конфигурации, включая работу с флагами и переменными окружения |
||
fs := flag.NewFlagSet(args[0], flag.PanicOnError) | ||
fs.SetOutput(os.Stdout) | ||
fs.StringVar(&c.Address, "a", ":8080", "server endpoint (ip:port)") | ||
fs.IntVar(&c.Backup.StoreInterval, "i", 300, "Store interval") | ||
fs.StringVar(&c.Backup.FileStoragePath, "f", "/tmp/metrics-db.json", "File storage path") | ||
fs.BoolVar(&c.Backup.Restore, "r", true, "Restore storage before start") | ||
fs.StringVar(&c.DB.DSN, "d", "", "Database DSN") | ||
fs.StringVar(&c.Hash.Key, "k", "", "SHA256 Key") | ||
if err := fs.Parse(args[1:]); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Используй структурированную обработку ошибок вместо паники при ошибке парсинга флагов |
||
panic(err.Error()) | ||
} | ||
var err error | ||
if v := os.Getenv("PROFILING"); v != "" { | ||
c.Profiling.Enabled, err = strconv.ParseBool(v) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Используй константы вместо магических чисел для значений по умолчанию флагов