Skip to content

Commit b9ad1ba

Browse files
committed
fix: edition URL when mkdocs.yml is not in the project root.
1 parent f22cdb5 commit b9ad1ba

File tree

4 files changed

+139
-36
lines changed

4 files changed

+139
-36
lines changed

core/core.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io/ioutil"
55
"log"
66
"os"
7+
"path"
78
"path/filepath"
89
"strings"
910

@@ -192,7 +193,7 @@ func getDocumentationRoot(repositoryRoot string) (string, error) {
192193
}
193194
}
194195

195-
return "", errors.Errorf("no file %s found in %s (search path was: %s)", manifest.FileName, repositoryRoot, strings.Join(docsRootSearchPaths, ","))
196+
return "", errors.Errorf("no file %s found in %s (search path was: %s)", manifest.FileName, repositoryRoot, strings.Join(docsRootSearchPaths, ", "))
196197
}
197198

198199
func buildDocumentation(branches []string, versionsInfo types.VersionsInformation,
@@ -246,11 +247,22 @@ func addEditionURI(config *types.Configuration, versionsInfo types.VersionsInfor
246247
return err
247248
}
248249

249-
manifest.AddEditionURI(manif, versionsInfo.Current, true)
250+
docsDirSuffix := getDocsDirSuffix(versionsInfo)
251+
252+
manifest.AddEditionURI(manif, versionsInfo.Current, docsDirSuffix, true)
250253

251254
return manifest.Write(manifestFile, manif)
252255
}
253256

257+
func getDocsDirSuffix(versionsInfo types.VersionsInformation) string {
258+
parts := strings.SplitN(versionsInfo.CurrentPath, string(filepath.Separator)+versionsInfo.Current+string(filepath.Separator), 2)
259+
if len(parts) <= 1 {
260+
return ""
261+
}
262+
263+
return path.Clean(strings.ReplaceAll(parts[1], string(filepath.Separator), "/"))
264+
}
265+
254266
// copyVersionSiteToOutputSite adds the generated documentation for the version described in ${versionsInfo} to the output directory.
255267
// If the current version (branch) name is related to the latest tag, then it's copied at the root of the output directory.
256268
// Else it is copied under a directory named after the version, at the root of the output directory.

core/core_test.go

+99-30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"testing"
88

9+
"github.com/containous/structor/types"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -52,40 +53,48 @@ func Test_getDocumentationRoot(t *testing.T) {
5253
defer func() { _ = os.RemoveAll(workingDirBasePath) }()
5354
require.NoError(t, err)
5455

56+
type expected struct {
57+
docsRoot string
58+
error string
59+
}
60+
5561
testCases := []struct {
56-
desc string
57-
workingDirectory string
58-
repositoryFiles []string
59-
expectedDocsRoot string
60-
expectedErrorMessage string
62+
desc string
63+
workingDirectory string
64+
repositoryFiles []string
65+
expected expected
6166
}{
6267
{
63-
desc: "working case with mkdocs in the root of the repository",
64-
workingDirectory: filepath.Join(workingDirBasePath, "mkdocs-in-root"),
65-
repositoryFiles: []string{"mkdocs.yml", "requirements.txt", "docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
66-
expectedDocsRoot: filepath.Join(workingDirBasePath, "mkdocs-in-root"),
67-
expectedErrorMessage: "",
68+
desc: "working case with mkdocs in the root of the repository",
69+
workingDirectory: filepath.Join(workingDirBasePath, "mkdocs-in-root"),
70+
repositoryFiles: []string{"mkdocs.yml", "requirements.txt", "docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
71+
expected: expected{
72+
docsRoot: filepath.Join(workingDirBasePath, "mkdocs-in-root"),
73+
},
6874
},
6975
{
70-
desc: "working case with mkdocs in ./docs",
71-
workingDirectory: filepath.Join(workingDirBasePath, "mkdocs-in-docs"),
72-
repositoryFiles: []string{"docs/mkdocs.yml", "docs/requirements.txt", "docs/docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
73-
expectedDocsRoot: filepath.Join(workingDirBasePath, "mkdocs-in-docs", "docs"),
74-
expectedErrorMessage: "",
76+
desc: "working case with mkdocs in ./docs",
77+
workingDirectory: filepath.Join(workingDirBasePath, "mkdocs-in-docs"),
78+
repositoryFiles: []string{"docs/mkdocs.yml", "docs/requirements.txt", "docs/docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
79+
expected: expected{
80+
docsRoot: filepath.Join(workingDirBasePath, "mkdocs-in-docs", "docs"),
81+
},
7582
},
7683
{
77-
desc: "error case with no mkdocs file found in the search path",
78-
workingDirectory: filepath.Join(workingDirBasePath, "no-mkdocs-in-search-path"),
79-
repositoryFiles: []string{"documentation/mkdocs.yml", "documentation/requirements.txt", "documentation/docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
80-
expectedDocsRoot: "",
81-
expectedErrorMessage: "no file mkdocs.yml found in " + workingDirBasePath + "/no-mkdocs-in-search-path (search path was: /,docs/)",
84+
desc: "error case with no mkdocs file found in the search path",
85+
workingDirectory: filepath.Join(workingDirBasePath, "no-mkdocs-in-search-path"),
86+
repositoryFiles: []string{"documentation/mkdocs.yml", "documentation/requirements.txt", "documentation/docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
87+
expected: expected{
88+
error: "no file mkdocs.yml found in " + workingDirBasePath + "/no-mkdocs-in-search-path (search path was: /, docs/)",
89+
},
8290
},
8391
{
84-
desc: "error case with no mkdocs file found at all",
85-
workingDirectory: filepath.Join(workingDirBasePath, "no-mkdocs-at-all"),
86-
repositoryFiles: []string{"docs/requirements.txt", "docs/docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
87-
expectedDocsRoot: "",
88-
expectedErrorMessage: "no file mkdocs.yml found in " + workingDirBasePath + "/no-mkdocs-at-all (search path was: /,docs/)",
92+
desc: "error case with no mkdocs file found at all",
93+
workingDirectory: filepath.Join(workingDirBasePath, "no-mkdocs-at-all"),
94+
repositoryFiles: []string{"docs/requirements.txt", "docs/docs.Dockerfile", ".gitignore", "docs/index.md", ".github/ISSUE.md"},
95+
expected: expected{
96+
error: "no file mkdocs.yml found in " + workingDirBasePath + "/no-mkdocs-at-all (search path was: /, docs/)",
97+
},
8998
},
9099
}
91100

@@ -108,18 +117,78 @@ func Test_getDocumentationRoot(t *testing.T) {
108117
}
109118
}
110119

111-
resultingDocsRoot, resultingError := getDocumentationRoot(test.workingDirectory)
120+
docsRoot, err := getDocumentationRoot(test.workingDirectory)
112121

113-
if test.expectedErrorMessage != "" {
114-
assert.EqualError(t, resultingError, test.expectedErrorMessage)
122+
if test.expected.error != "" {
123+
assert.EqualError(t, err, test.expected.error)
115124
} else {
116-
require.NoError(t, resultingError)
117-
assert.Equal(t, test.expectedDocsRoot, resultingDocsRoot)
125+
require.NoError(t, err)
126+
assert.Equal(t, test.expected.docsRoot, docsRoot)
118127
}
119128
})
120129
}
121130
}
122131

132+
func Test_getDocsDirSuffix(t *testing.T) {
133+
testCases := []struct {
134+
desc string
135+
version types.VersionsInformation
136+
expected string
137+
}{
138+
{
139+
desc: "no suffix",
140+
version: types.VersionsInformation{
141+
CurrentPath: "/tmp/structor694968263/v2.0",
142+
Current: "v2.0",
143+
},
144+
expected: "",
145+
},
146+
{
147+
desc: "simple suffix",
148+
version: types.VersionsInformation{
149+
CurrentPath: "/tmp/structor694968263/v2.0/docs",
150+
Current: "v2.0",
151+
},
152+
expected: "docs",
153+
},
154+
{
155+
desc: "suffix with slash",
156+
version: types.VersionsInformation{
157+
CurrentPath: "/tmp/structor694968263/v2.0/docs/",
158+
Current: "v2.0",
159+
},
160+
expected: "docs",
161+
},
162+
{
163+
desc: "long suffix",
164+
version: types.VersionsInformation{
165+
CurrentPath: "/tmp/structor694968263/v2.0/docs/foo",
166+
Current: "v2.0",
167+
},
168+
expected: "docs/foo",
169+
},
170+
{
171+
desc: "contains two times the version path",
172+
version: types.VersionsInformation{
173+
CurrentPath: "/tmp/structor694968263/v2.0/docs/foo/v2.0/bar",
174+
Current: "v2.0",
175+
},
176+
expected: "docs/foo/v2.0/bar",
177+
},
178+
}
179+
180+
for _, test := range testCases {
181+
test := test
182+
t.Run(test.desc, func(t *testing.T) {
183+
t.Parallel()
184+
185+
suffix := getDocsDirSuffix(test.version)
186+
187+
assert.Equal(t, test.expected, suffix)
188+
})
189+
}
190+
}
191+
123192
func Test_createDirectory(t *testing.T) {
124193
dir, err := ioutil.TempDir("", "structor-test")
125194
require.NoError(t, err)

manifest/manifest.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package manifest
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
6+
"path"
77
"path/filepath"
88

99
"github.com/pkg/errors"
@@ -80,7 +80,7 @@ func AppendExtraCSS(manif map[string]interface{}, cssFile string) {
8080
}
8181

8282
// AddEditionURI Adds an edition URI to the "edit_uri" in the manifest file.
83-
func AddEditionURI(manif map[string]interface{}, version string, override bool) {
83+
func AddEditionURI(manif map[string]interface{}, version string, docsDirBase string, override bool) {
8484
v := version
8585
if v == "" {
8686
v = "master"
@@ -93,5 +93,5 @@ func AddEditionURI(manif map[string]interface{}, version string, override bool)
9393

9494
docsDir := getDocsDirAttribute(manif)
9595

96-
manif["edit_uri"] = fmt.Sprintf("edit/%s/%s/", v, docsDir)
96+
manif["edit_uri"] = path.Join("edit", v, docsDirBase, docsDir) + "/"
9797
}

manifest/manifest_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func TestAddEditionURI(t *testing.T) {
287287
desc string
288288
manif map[string]interface{}
289289
version string
290+
baseDir string
290291
override bool
291292
expected map[string]interface{}
292293
}{
@@ -336,14 +337,35 @@ func TestAddEditionURI(t *testing.T) {
336337
"edit_uri": "edit/v2/docs/",
337338
},
338339
},
340+
{
341+
desc: "version, no override, base dir",
342+
manif: map[string]interface{}{
343+
"edit_uri": "edit/v1/docs/"},
344+
version: "v2",
345+
baseDir: "foo",
346+
expected: map[string]interface{}{
347+
"edit_uri": "edit/v1/docs/",
348+
},
349+
},
350+
{
351+
desc: "version, override, base dir",
352+
manif: map[string]interface{}{
353+
"edit_uri": "edit/v1/docs/"},
354+
version: "v2",
355+
baseDir: "foo",
356+
override: true,
357+
expected: map[string]interface{}{
358+
"edit_uri": "edit/v2/foo/docs/",
359+
},
360+
},
339361
}
340362

341363
for _, test := range testCases {
342364
test := test
343365
t.Run(test.desc, func(t *testing.T) {
344366
t.Parallel()
345367

346-
AddEditionURI(test.manif, test.version, test.override)
368+
AddEditionURI(test.manif, test.version, test.baseDir, test.override)
347369

348370
assert.Equal(t, test.expected, test.manif)
349371
})

0 commit comments

Comments
 (0)