1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use schema_core::json_rpc::types::*;
use std::path::Path;
use tempfile::TempDir;

#[must_use = "This struct does nothing on its own. See ListMigrationDirectories::send()"]
pub struct ListMigrationDirectories<'a> {
    migrations_directory: &'a TempDir,
}

impl<'a> ListMigrationDirectories<'a> {
    pub fn new(migrations_directory: &'a TempDir) -> Self {
        ListMigrationDirectories { migrations_directory }
    }

    #[track_caller]
    pub fn send(self) -> ListMigrationDirectoriesAssertion<'a> {
        let migrations_from_filesystem = schema_core::schema_connector::migrations_directory::list_migrations(
            Path::new(self.migrations_directory.path()),
        )
        .unwrap();

        let migrations = migrations_from_filesystem
            .iter()
            .map(|migration| migration.migration_name().to_string())
            .collect();

        let output = ListMigrationDirectoriesOutput { migrations };

        ListMigrationDirectoriesAssertion {
            output,
            _migrations_directory: self.migrations_directory,
        }
    }
}

pub struct ListMigrationDirectoriesAssertion<'a> {
    output: ListMigrationDirectoriesOutput,
    _migrations_directory: &'a TempDir,
}

impl std::fmt::Debug for ListMigrationDirectoriesAssertion<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ListMigrationDirectoriesAssertion {{ .. }}")
    }
}

impl<'a> ListMigrationDirectoriesAssertion<'a> {
    #[track_caller]
    pub fn assert_listed_directories(self, names: &[&str]) -> Self {
        let found_names: Vec<&str> = self.output.migrations.iter().map(|name| &name[15..]).collect();

        assert!(
            found_names == names,
            "Assertion failed. The listed migrations do not match the expectations. ({found_names:?} vs {names:?})"
        );

        self
    }
}