Repository: getnikola/nikola

Issue:
`check -l` can’t stomach REDIRECTIONS pages
```
REDIRECTIONS = [ ( "test.html", "/index.html" ) ]

WARNING: check: Broken link in output/test.html: 0; url=/index.html
WARNING: check: Broken link in output/test.html: /index.html
```



Discussion:
For the second warning, problem is this:

```
target = '/index.html'
```

We create the filename that we check for existence like this:

```
os.path.join(os.path.dirname(filename), target)
```

Since target is an absolute path, this returns `/index.html` instead of `output/index.html`.

For the 1st line ... looks like lxml has no idea how to parse that, workaround coming up.

Still seeing both warnings.

Correction, I’m now seeing:

```
WARNING: check: Broken link in output/test.html: index.html
WARNING: check: Broken link in output/test.html: index.html
```

Hey, does output/index.html exist?

It does indeed.

Then I can't reproduce.

I haven’t got time right now to verify this, but what about REDIRECTIONS = [ ( "folder1/test.html", "/folder2/test.html" ) ]
@ralsina, you just removed the leading slash, right? so it’s looking for folder1/folder2/test.html and not  folder2/test.html relative to root.

If you do a redirect from folder1/test.html to folder2/test.html it _will_ point to folder1/folder2/test.html right?

Considering we use the target of the redirect as-is, it should.

Yes, but the link pointed to /folder2/test.html before you `target.lstrip('/')` on line 241. I think that is what happens. So by lstripping you change check.py’s interpretation of the link from /folder2/test.html to /folder1/folder2/test.html 

But then the redirection would be ``[("folder1/test.html", "/folder2/test.html")], right?

Ok, I see how I have to differentiate here. I'll do a fix in a bit.

Right, which it was supposed to be. Typing out examples from memory on a mobile while on the go was probably a bad idea. Sorry about the added confusion from me not checking my typing whilst not contributing anything but complaints. ;)

```
  File "/usr/local/lib/python3.4/dist-packages/nikola/plugins/command/check.py", line 242
    else:  # Relative path
       ^
SyntaxError: invalid syntax
```

And I should not try to code without even running the code before pushing ;-)

REDIRECTS = [ ("posts/review-the-martian.html", "/book-review/the-martian.html"), ]

Produced: [/posts/review-the-martian.html](https://www.aeyoun.com/posts/review-the-martian.html) which successfully redirects to [/book-review/the-martian.html](https://www.aeyoun.com/book-review/the-martian.html) (which also exists).

check -l still disagrees:

[2015-06-01T20:01:24Z] WARNING: check: Broken link in output/posts/review-the-martian.html: /book-review/the-martian.html
[2015-06-01T20:01:24Z] WARNING: check: Broken link in output/posts/review-the-martian.html: /book-review/the-martian.html

Ok, so this will need actual work. Sorry for wasting time with halff-assed work :-(


Resolving patch:
diff --git a/nikola/plugins/command/check.py b/nikola/plugins/command/check.py
--- a/nikola/plugins/command/check.py
+++ b/nikola/plugins/command/check.py
@@ -238,7 +238,7 @@ def analyze(self, fname, find_sources=False, check_remote=False):
                 if url_type == 'rel_path':
                     if target.startswith('/'):
                         target_filename = os.path.abspath(
-                            os.path.join(os.path.dirname(filename), unquote(target.lstrip('/'))))
+                            os.path.join(self.site.config['OUTPUT_FOLDER'], unquote(target.lstrip('/'))))
                     else:  # Relative path
                         target_filename = os.path.abspath(
                             os.path.join(os.path.dirname(filename), unquote(target)))


Test patch:
diff --git a/tests/test_integration.py b/tests/test_integration.py
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -545,5 +545,50 @@ def test_invariance(self):
                 self.assertEqual(exc.returncode, 0, 'Unexplained diff for the invariance test.')
 
 
+class RedirectionsTest1(TestCheck):
+    """Check REDIRECTIONS"""
+
+    @classmethod
+    def patch_site(self):
+        """"""
+        conf_path = os.path.join(self.target_dir, "conf.py")
+        with io.open(conf_path, "a", encoding="utf8") as outf:
+            outf.write("""\n\nREDIRECTIONS = [ ("posts/foo.html", "/foo/bar.html"), ]\n\n""")
+
+    @classmethod
+    def fill_site(self):
+        target_path = os.path.join(self.target_dir, "files", "foo", "bar.html")
+        nikola.utils.makedirs(os.path.join(self.target_dir, "files", "foo"))
+        with io.open(target_path, "w+", encoding="utf8") as outf:
+            outf.write("foo")
+
+class RedirectionsTest2(TestCheck):
+    """Check external REDIRECTIONS"""
+
+    @classmethod
+    def patch_site(self):
+        """"""
+        conf_path = os.path.join(self.target_dir, "conf.py")
+        with io.open(conf_path, "a", encoding="utf8") as outf:
+            outf.write("""\n\nREDIRECTIONS = [ ("foo.html", "http://www.example.com/"), ]\n\n""")
+
+class RedirectionsTest3(TestCheck):
+    """Check relative REDIRECTIONS"""
+
+    @classmethod
+    def patch_site(self):
+        """"""
+        conf_path = os.path.join(self.target_dir, "conf.py")
+        with io.open(conf_path, "a", encoding="utf8") as outf:
+            outf.write("""\n\nREDIRECTIONS = [ ("foo.html", "foo/bar.html"), ]\n\n""")
+
+    @classmethod
+    def fill_site(self):
+        target_path = os.path.join(self.target_dir, "files", "foo", "bar.html")
+        nikola.utils.makedirs(os.path.join(self.target_dir, "files", "foo"))
+        with io.open(target_path, "w+", encoding="utf8") as outf:
+            outf.write("foo")
+
+
 if __name__ == "__main__":
     unittest.main()