# name: test/extension/install_extension.test_slow
# description: Try loading a shared object with an extension
# group: [extension]

require notmingw

require skip_reload

statement ok
PRAGMA enable_verification

# we need a persistent database because otherwise we are not allowed to restart
load __TEST_DIR__/dummy_database_for_extension_installation.db

statement error
INSTALL 'asdf';
----

statement error
INSTALL 'asdf/asdf.duckdb_extension';
----

statement ok
INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';

statement ok
FORCE INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';

statement ok
LOAD 'loadable_extension_demo';

statement ok
LOAD 'loadable_extension_demo';

# need to restart to unload extensions
restart

# this will succeed on MacOS due to case insensitive filesystem
statement maybe
FORCE INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_DEMO.duckdb_extension';
----
IO Error: Failed to install local extension

statement ok
FORCE INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';

statement ok
LOAD 'Loadable_Extension_DEMO';

# need to restart to unload extensions
restart

# can't find extension in non-default extension location
statement ok
SET extension_directory='__TEST_DIR__/extension_directory'

statement ok
SET extension_directories=[]

statement error
LOAD 'loadable_extension_demo';
----

# can install after setting directory, even if the extension directory does not yet exist
statement ok
INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';

# can load now
statement ok
LOAD 'loadable_extension_demo';

# ensure file is there
query I
select count(*) from glob('__TEST_DIR__/extension_directory/**/loadable_extension_demo.duckdb_extension')
----
1

# unload again
restart

# Ensure that using an array of extension directories works.
statement ok
SET extension_directory=''

statement ok
SET extension_directories=['__TEST_DIR__/extension_directory_a', '__TEST__DIR__/second_extension_directory_b']

statement error
LOAD 'loadable_extension_demo';
----

# can install after setting directory, even if the extension directory does not yet exist
statement ok
INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';

# can load now
statement ok
LOAD 'loadable_extension_demo';

# Switching the order shouldn't make a difference
statement ok
SET extension_directories=['__TEST_DIR__/extension_directory_b', '__TEST__DIR__/second_extension_directory_a']

statement ok
LOAD 'loadable_extension_demo';


# unload again
restart


statement ok
SET extension_directory=''

statement ok
SET extension_directories=[]

# we will use a nonstandard home directory, but we won't create it if it does not exist

# can't find extension in non-default home location
statement ok
SET home_directory='__TEST_DIR__/home_directory'

statement error
LOAD 'loadable_extension_demo';
----

# we can't install because we will not create a non-existing home directory
statement error
INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';
----

# but this one should exist
statement ok
SET home_directory='__TEST_DIR__'

statement ok
INSTALL '__BUILD_DIRECTORY__/test/extension/loadable_extension_demo.duckdb_extension';

query I
select COUNT(*) from glob('__TEST_DIR__/.duckdb/**/loadable_extension_demo.duckdb_extension')
----
1

# can load now
statement ok
LOAD 'loadable_extension_demo';
