Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 76 additions & 3 deletions src/org/rascalmpl/library/lang/rascal/tests/library/IO.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,79 @@ import IO;
import DateTime;
import String;

test bool testLogicalLocationResolution() {
str scheme = "test-logical";

value exceptionOf(value() f) {
try {
f();
return "";
} catch e: {
return e;
}
}

bool throwsUnsupportedAuthority(value() f) {
return /Unsupported authority/ := "<exceptionOf(f)>";
}

bool throwsExceptionDownstream(value() f) {
return str s := "<exceptionOf(f)>" && "" != s && /Unsupported authority/ !:= s;
}

try {
// Register authorities `foo` and `bar`
registerLocations(scheme, "foo", (
|<scheme>://foo/|: |file:///|,
|<scheme>://foo/x/y/z|: |file:///x/y/z|
));
registerLocations(scheme, "bar", (
|<scheme>://bar/|: |<scheme>://foo/|,
|<scheme>://bar/x/y/z|: |<scheme>://foo/x/y/z|
));

assert lastModified(|<scheme>://foo/|) == lastModified(|file:///|) : "Resolution unexpectedly failed";
assert lastModified(|<scheme>://bar/|) == lastModified(|file:///|) : "Resolution unexpectedly failed";

str cause = "Resolution either unexpectedly succeeded or failed with an unexpected exception";
assert throwsExceptionDownstream(value() { return lastModified(|<scheme>://foo/x/y/z|); }) : cause;
assert throwsExceptionDownstream(value() { return lastModified(|<scheme>://bar/x/y/z|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>://baz/|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>://baz/x/y/z|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>://qux/|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>://qux/x/y/z|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>:///|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>:///x/y/z|); }) : cause;

// Register default authority
registerLocations(scheme, "", (
|<scheme>://baz/|: |<scheme>://bar/|,
|<scheme>://baz/x/y/z|: |<scheme>://bar/x/y/z|
));

assert lastModified(|<scheme>://foo/|) == lastModified(|file:///|) : "Resolution unexpectedly failed";
assert lastModified(|<scheme>://bar/|) == lastModified(|file:///|) : "Resolution unexpectedly failed";
assert lastModified(|<scheme>://baz/|) == lastModified(|file:///|) : "Resolution unexpectedly failed";

assert throwsExceptionDownstream(value() { return lastModified(|<scheme>://foo/x/y/z|); }) : cause;
assert throwsExceptionDownstream(value() { return lastModified(|<scheme>://bar/x/y/z|); }) : cause;
assert throwsExceptionDownstream(value() { return lastModified(|<scheme>://baz/x/y/z|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>://qux/|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>://qux/x/y/z|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>:///|); }) : cause;
assert throwsUnsupportedAuthority(value() { return lastModified(|<scheme>:///x/y/z|); }) : cause;

return true;
}
catch false: // Catch block only to make finally block grammatical
throw false;
finally {
unregisterLocations(scheme, "foo");
unregisterLocations(scheme, "bar");
unregisterLocations(scheme, "");
}
}

test bool testFileCopyCompletely() {
writeFile(|tmp:///longFile|, "123456789");
writeFile(|tmp:///shortFile|, "321");
Expand All @@ -22,9 +95,9 @@ test bool testFileCopyRecursive() {

test bool watchDoesNotCrashOnURIRewrites() {
writeFile(|tmp:///watchDoesNotCrashOnURIRewrites/someFile.txt|, "123456789");
watch(|tmp:///watchDoesNotCrashOnURIRewrites|, true, void (FileSystemChange event) {
watch(|tmp:///watchDoesNotCrashOnURIRewrites|, true, void (FileSystemChange event) {
// this should trigger the failing test finally
remove(event.file);
remove(event.file);
});
return true;
}
Expand Down Expand Up @@ -75,4 +148,4 @@ test bool renameDirectory() {
writeFile(|tmp:///Folder/hello.txt|, "Hello World!");
rename(|tmp:///Folder|, |tmp:///RenamedFolder|);
return readFile(|tmp:///RenamedFolder/hello.txt|) == "Hello World!";
}
}
24 changes: 23 additions & 1 deletion src/org/rascalmpl/uri/ILogicalSourceLocationResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@

import java.io.IOException;

import org.checkerframework.checker.nullness.qual.Nullable;

import io.usethesource.vallang.ISourceLocation;

/**
* Resolver of logical locations with a certain {@link #scheme} and {@link #authority}. Implementations of this
* interface for non-empty authorities are <em>authority-specific resolvers</em>. Implementations for the distinguished
* empty authority are <em>default resolvers</em>. To resolve a logical location, first, an authority-specific resolver
* for the scheme is sought. If it doesn't exist, then the default resolver for the scheme is sought instead. If it
* doesn't exist either, then an exception is thrown. Note: If an authority-specific resolver does exist, but fails,
* then the default resolver will not be sought instead (i.e., the default resolver resembles the default case of a
* switch statement with non-fallthrough cases).
*/
public interface ILogicalSourceLocationResolver {
ISourceLocation resolve(ISourceLocation input) throws IOException;

/**
* @return {@code null} only if the authority of {@code input} is not supported/covered by this resolver.
* Non-{@code null} otherwise.
*/
@Nullable ISourceLocation resolve(ISourceLocation input) throws IOException;

String scheme();

/**
* @return A non-empty string if this is an authority-specific resolver. The empty string if this is a default
* resolver.
*/
String authority();
}
Loading
Loading