Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from kachick/cover-flake
Browse files Browse the repository at this point in the history
Implement to cover flake.nix
  • Loading branch information
kachick committed Jul 15, 2023
2 parents e4e11f2 + 72763dd commit cf48bdb
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 21 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ on:
push:
branches:
- main
paths:
- '**.go'
- 'go.*'
- 'testdata/**'
pull_request:
paths:
- '**.go'
- 'go.*'
- 'testdata/**'

jobs:
build:
Expand All @@ -31,3 +39,14 @@ jobs:
run: go vet
- name: format
run: go fmt && git add --intent-to-add . && git diff --exit-code
# Because of default in vscode extension
staticcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- uses: dominikh/staticcheck-action@v1.3.0
with:
# Keep same version as *.nix
version: '2023.1.3'
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
- uses: dprint/check@v2.2
with:
# Keep same version as *.nix
dprint-version: '0.36.1'
dprint-version: '0.37.1'

typos:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Keep same version as *.nix
- uses: crate-ci/typos@v1.15.0
- uses: crate-ci/typos@v1.16.1
with:
files: |
.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

For my personal use.

I'm a new to the Nix ecosystem.
I'm a new to the Nix ecosystem.\
(If you know a better way, please let me know!)

I have `default.nix` and `shell.nix` in many repositories. They have different nixpath(?) in the ref from the created timing.
Personally, I use the latest [nixpkgs](https://github.com/NixOS/nixpkgs) ref.
I have `flake.nix` and `default.nix` in many repositories. They have different nixpath(?) in the ref from the created timing.\
Personally, I use the latest [nixpkgs](https://github.com/NixOS/nixpkgs) ref. But I avoid to specify `unstable`.\
When I want to bump it, I always visit the nixpkgs repository and copy and paste. It is a tedious task.

## Installation
Expand Down
2 changes: 2 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tasks:
- goreleaser check
- typos . .github .vscode
- nixpkgs-fmt --check ./*.nix
- staticcheck
versions:
cmds:
- nix --version
Expand All @@ -39,3 +40,4 @@ tasks:
- dprint --version
- goreleaser --version
- typos --version
- staticcheck --version
2 changes: 1 addition & 1 deletion cmd/nix-headbump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ $ nix-headbump -version`
log.Fatalf("Failed to get target files: %s", err.Error())
}
if path == "" {
log.Fatalln("Both default.nix and shell.nix are not found")
log.Fatalln("Any *.nix files are not found")
}

switch os.Args[1] {
Expand Down
23 changes: 20 additions & 3 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"regexp"
)

var (
re = regexp.MustCompile(`(?s)(import\s+\(fetchTarball\s+"https://github.com/NixOS/nixpkgs/archive/)([^"]+?)(\.tar\.gz"\))`)
importRe = regexp.MustCompile(`(?s)(import\s+\(fetchTarball\s+"https://github.com/NixOS/nixpkgs/archive/)([^"]+?)(\.tar\.gz"\))`)
flakeRe = regexp.MustCompile(`(nixpkgs\.url\s+=\s+"github:NixOS/nixpkgs/)([^"]+)(")`)
)

// Returned regex should have 3 size captured groups
func GetRegexp(path string) *regexp.Regexp {
if filepath.Base(path) == "flake.nix" {
return flakeRe
}

return importRe
}

func Bump(path string, last string) error {
origin, err := os.ReadFile(path)
if err != nil {
return err
}
re := GetRegexp(path)
replaced := re.ReplaceAll(origin, []byte("${1}"+last+"${3}"))
if bytes.Equal(origin, replaced) {
return nil
Expand All @@ -28,15 +40,15 @@ func Bump(path string, last string) error {
}

func GetTargetPath() (string, error) {
paths := [2]string{"default.nix", "shell.nix"}
paths := [3]string{"flake.nix", "default.nix", "shell.nix"}
for _, path := range paths {
_, err := os.Stat(path)
if err == nil {
return path, nil
}

if !os.IsNotExist(err) {
return "", fmt.Errorf("Can not open %s: %w", path, err)
return "", fmt.Errorf("can not open %s: %w", path, err)
}
}
return "", fmt.Errorf("%v are not found", paths)
Expand All @@ -47,7 +59,12 @@ func GetCurrentVersion(path string) (string, error) {
if err != nil {
return "", err
}
re := GetRegexp(path)
matches := re.FindStringSubmatch(string(origin))
if err != nil || len(matches) < 2 {
return "", err
}

return matches[2], nil
}

Expand Down
84 changes: 80 additions & 4 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestGetCurrentVersion(t *testing.T) {
func TestGetCurrentVersionForFlakeNix(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
Expand All @@ -16,7 +16,34 @@ func TestGetCurrentVersion(t *testing.T) {
t.Fatalf("failed to rollback working directory: %v", err)
}
})
err = os.Chdir("testdata")
err = os.Chdir("testdata/flake")
if err != nil {
t.Fatalf("failed to walk through testdata directory: %v", err)
}

got, err := GetCurrentVersion("flake.nix")
if err != nil {
t.Fatalf("Getting the version has been failed: %s", err.Error())
}
want := "e57b65abbbf7a2d5786acc86fdf56cde060ed026"

if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestGetCurrentVersionForDefaultNix(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
}
t.Cleanup(func() {
err := os.Chdir(cwd)
if err != nil {
t.Fatalf("failed to rollback working directory: %v", err)
}
})
err = os.Chdir("testdata/classic")
if err != nil {
t.Fatalf("failed to walk through testdata directory: %v", err)
}
Expand All @@ -32,6 +59,28 @@ func TestGetCurrentVersion(t *testing.T) {
}
}

func TestGetCurrentVersionInEmptyDir(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
}
t.Cleanup(func() {
err := os.Chdir(cwd)
if err != nil {
t.Fatalf("failed to rollback working directory: %v", err)
}
})
err = os.Chdir("testdata/nothing")
if err != nil {
t.Fatalf("failed to walk through testdata directory: %v", err)
}

_, err = GetCurrentVersion("default.nix")
if !os.IsNotExist(err) {
t.Errorf("returned unexpected error: %v", err)
}
}

// Calling actual GitHub API, May be necessary to stub or disabling in CI
func TestGetLastVersion(t *testing.T) {
got, err := GetLastVersion()
Expand All @@ -45,7 +94,7 @@ func TestGetLastVersion(t *testing.T) {
}
}

func TestTargetPath(t *testing.T) {
func TestClassicTargetPath(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
Expand All @@ -56,7 +105,7 @@ func TestTargetPath(t *testing.T) {
t.Fatalf("failed to rollback working directory: %v", err)
}
})
err = os.Chdir("testdata")
err = os.Chdir("testdata/classic")
if err != nil {
t.Fatalf("failed to walk through testdata directory: %v", err)
}
Expand All @@ -71,3 +120,30 @@ func TestTargetPath(t *testing.T) {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestTargetPathMultipleCandidates(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
}
t.Cleanup(func() {
err := os.Chdir(cwd)
if err != nil {
t.Fatalf("failed to rollback working directory: %v", err)
}
})
err = os.Chdir("testdata/candidates")
if err != nil {
t.Fatalf("failed to walk through testdata directory: %v", err)
}

got, err := GetTargetPath()
if err != nil {
t.Fatalf("Failed to get target files: %s", err.Error())
}
want := "flake.nix"

if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
6 changes: 3 additions & 3 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"includes": ["**/*.{json,md,yml}"],
"excludes": ["dist"],
"plugins": [
"https://plugins.dprint.dev/json-0.17.1.wasm",
"https://plugins.dprint.dev/markdown-0.15.2.wasm",
"https://plugins.dprint.dev/prettier-0.24.0.json@9a57d0d8e440ad90d07a503166af47e7a6a886abd46767933f9c279f72468596"
"https://plugins.dprint.dev/json-0.17.4.wasm",
"https://plugins.dprint.dev/markdown-0.15.3.wasm",
"https://plugins.dprint.dev/prettier-0.26.1.json@fdbe31f6aecd24f9d6b924214710a6766050d03146163b4e241e6056b2462f2e"
]
}
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/e57b65abbbf7a2d5786acc86fdf56cde060ed026";
nixpkgs.url = "github:NixOS/nixpkgs/7fd307937db70af23b956c4539033542809ae263";
flake-utils.url = "github:numtide/flake-utils";
};

Expand All @@ -21,6 +21,7 @@
go-task
goreleaser
typos
go-tools
];
};
});
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions testdata/candidates/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/e57b65abbbf7a2d5786acc86fdf56cde060ed026";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.default = with pkgs;
mkShell {
buildInputs = [
go_1_20
go-task
goreleaser
];
};
});
}
7 changes: 7 additions & 0 deletions testdata/candidates/home.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ config, pkgs, ... }:

{
# Home Manager needs a bit of information about you and the
# paths it should manage.
home.username = "kachick";
}
9 changes: 9 additions & 0 deletions testdata/candidates/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/e57b65abbbf7a2d5786acc86fdf56cde060ed026.tar.gz") { } }:

pkgs.mkShell {
buildInputs = [
pkgs.go_1_20
pkgs.go-task
pkgs.goreleaser
];
}
9 changes: 9 additions & 0 deletions testdata/classic/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/e57b65abbbf7a2d5786acc86fdf56cde060ed026.tar.gz") { } }:

pkgs.mkShell {
buildInputs = [
pkgs.go_1_20
pkgs.go-task
pkgs.goreleaser
];
}
22 changes: 22 additions & 0 deletions testdata/flake/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/e57b65abbbf7a2d5786acc86fdf56cde060ed026";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.default = with pkgs;
mkShell {
buildInputs = [
go_1_20
go-task
goreleaser
];
};
});
}
Empty file added testdata/nothing/.gitkeep
Empty file.

0 comments on commit cf48bdb

Please sign in to comment.