Or HaChaim notes that the presence of other nations served both as a test and a means to demonstrate G-d’s just handling of the land's occupants, creating a clear moral picture before Israel took possession.
אָכֵן כַּוָּנַת הַכָּתוּב הִיא עַל דֶּרֶךְ אָמְרָם זִכְרוֹנָם לִבְרָכָה בְּמַסֶּכֶת בְּרָכוֹת (ז:) פֶּרֶק רִאשׁוֹן בְּפָסוּק (חבקוק א:יג) ״בְּבַלַּע רָשָׁע צַדִּיק מִמֶּנּוּ״ – צַדִּיק מִמֶּנּוּ בּוֹלֵעַ, צַדִּיק גָּמוּר אֵינוֹ בּוֹלֵעַ, עַד כָּאן. פֵּרוּשׁ, כִּי אִם יִהְיֶה רָשָׁע עֲדַיִן יֵשׁ בְּיָדוֹ קְצָת זְכוּת, וְיָבֹא צַדִּיק שֶׁרֻבּוֹ זְכֻיּוֹת לְהִתְגָּרוֹת בּוֹ – אֵין כֹּחַ בַּצַּדִּיק לְאַבְּדוֹ, כִּי עֲדַיִן יֵשׁ לָרָשָׁע לְהִשְׁתַּלֵּם זְכוּת הַנִּשְׁאָר לוֹ בָּעוֹלָם הַזֶּה, וְהָרָשָׁע יִתְנַקֵּם מֵהַצַּדִּיק לֶהֱיוֹת שֶׁיֵּשׁ בְּיַד הַצַּדִּיק מֵהַתִּעוּב שֶׁאֵינוֹ צַדִּיק גָּמוּר. אֲבָל כְּשֶׁיִּהְיֶה גָּמוּר, הֲגַם שֶׁיִּשָּׁאֵר לָרָשָׁע חֵלֶק מֵהַטּוֹב – יֻפְקַע כְּלִילָתוֹ, וְכֵן אִם יִהְיֶה הָרָשָׁע מֻחְלָט וְנִגְמַר דִּינוֹ לָמוּת, בַּכֹּל עוֹשֶׂה הַשֵּׁם שְׁלִיחוּתוֹ לְאַבְּדוֹ, וְכָל הַקּוֹדֵם בּוֹ זָכָה, וַאֲפִלּוּ אֵינוֹ צַדִּיק גָּמוּר יָכוֹל לְאַבְּדוֹ מִן הָעוֹלָם. וְהִנֵּה בְּצֵאת יִשְׂרָאֵל מִמִּצְרַיִם וּכְשֶׁשָּׁלְחוּ הַמְרַגְּלִים הָיוּ בְּמַדְרֵגַת צַדִּיקִים גְּמוּרִים, וַהֲגַם שֶׁעָשׂוּ הָעֵגֶל, כְּבָר נֶעֶנְשׁוּ עָלָיו, וְהַשִּׁמְצָה אֵין בּוֹ שִׁעוּר שֶׁיִּמְנַע מֵהֶם שֵׁם צַדִּיק גָּמוּר לְעִנְיָן זֶה שֶׁלֹּא יִקָּרְאוּ צַדִּיקִים גְּמוּרִים בְּעֵרֶךְ הָאֱמוֹרִי, וְהֵן הֵנָּה דּוֹר חָסִיד (סנהדרין קי:) דִּכְתִיב (תהלים נ:ה) ״אִסְפוּ לִי חֲסִידָי כּוֹרְתֵי״ וְגוֹ׳ אֲשֶׁר לֹא כֵן הָיוּ אַחַר שְׁלִיחוּת הַמְרַגְּלִים שֶׁהִקְנִיטוּ אֶת ה׳ וְהִמְרוּ וְכוּ׳, וְאָז נִכְנְסוּ בְּגֶדֶר צַדִּיק מִמֶּנּוּ בְּעֵרֶךְ הָאֱמוֹרִי. וְלָכֵן כְּשֶׁהָיוּ צַדִּיקִים גְּמוּרִים קֹדֶם הַמְרַגְּלִים הָיָה בָּהֶם כֹּחַ לִדְחוֹת הָאֱמוֹרִי, כִּי יֹאמְרוּ אֵלָיו פַּנֵּה מָקוֹם מֵאַרְצֵנוּ אֲשֶׁר נָתַן לָנוּ ה׳, וּכְשֶׁלֹּא יִרְצֶה הֲרֵי זֶה מוֹרֵד וּבֶן מָוֶת הוּא, מַה שֶׁאֵין כֵּן אַחַר שֶׁחָטְאוּ וְהִמְרוּ אִמְרֵי אֵל לֹא נִשְׁאֲרוּ בְּחֶזְקַת צַדִּיק גָּמוּר. וְלֶהֱיוֹת שֶׁעֲדַיִן לֹא שָׁלֵם עֲוֹן הָאֱמוֹרִי אֵין בְּיָדָם לְהַשְׁמִידוֹ. וְלָזֶה תִּמְצָא כְּשֶׁהֶעְפִּילוּ לַעֲלוֹת הָהָרָה יָצָא הָאֱמוֹרִי וּרְדָפוּם וַיַּכּוּם כַּאֲשֶׁר תַּעֲשֶׂינָה הַדְּבוֹרִים, וְזֶה יַגִּיד בַּלַּע רָשָׁע צַדִּיק מִמֶּנּוּ. וְתִמְצָא עוֹד שֶׁבִּכְנִיסָתָם אָמַר מֹשֶׁה (דברים ט:ה): ״לֹא בְצִדְקָתְךָ וְגוֹ׳ כִּי אִם בְּרִשְׁעַת הַגּוֹיִם הָאֵלֶּה״, כִּי נִשְׁלַם עֲוֹנָם. וּכְפִי זֶה, הֲגַם שֶׁאֵינְכֶם צַדִּיקִים גְּמוּרִים, ״אַתָּה בָּא לָרֶשֶׁת״ וְגוֹ׳, כִּי אֵין לוֹ שׁוּם זְכוּת לֵיאָמֵר עָלָיו ״צַדִּיק מִמֶּנּוּ״. כִּי מַשְׁמָעוּת ״צַדִּיק מִמֶּנּוּ״ יוֹרֶה שֶׁיֵּשׁ לוֹ קְצָת זְכוּת שֶׁיֻּצְדַּק לֵיאָמֵר ״צַדִּיק מִמֶּנּוּ״, אֲבָל מֻחְלָט בְּרֶשַׁע אֵין לוֹ חֵלֶק הַצַּדְקוּת לֵיאָמֵר ״צַדִּיק מִמֶּנּוּ״, וְהָבֵן.
We must understand the Torah in terms of how the Talmud Berachot 7 explains Chabakuk 1,13: למה תביט בוגדים, תחריש בבלע רשע צדיק ממנו, "Why do You countenance treachery, and stand by idly when the one in the wrong devours the one in the right?" The Talmud says that while it is possible that the wicked triumphs over someone relatively more righteous than he, he will never triumph over someone truly righteous. This means that the wicked are not totally devoid of merits, and we find on occasion that by using their merit they can overcome someone else who is generally more pious than they (while on this earth). The reason is that the wicked at that time can point at the imperfections of the person who thinks of himself as righteous. When the wicked is absolutely wicked however, it is not only permissible to destroy him but whichever righteous person does so first deserves credit. One need not be a totally righteous person in order to be entitled to wipe out such a wicked person. At the time of the Exodus as well as at the time when they despatched the spies about 15 months later, the Israelites were on the level of perfectly righteous people. Although they had sinned during the episode of the golden calf, they had been punished for that sin and had regained their status as צדיקים גמורים, as perfectly righteous people. The "echo" of that sin was not strong enough to deprive them of that title especially vis-a-vis the Emorites. In Sanhedrin 110 Rabbi Eliezer describes the people at that time as pious, quoting Psalms 50,5: "bring in My devotees, who made a covenant with Me over sacrifice" as his support. After the episode with the spies, Israel lost this status. From that moment on their moral superiority over the Emorites was marginal. As long as they had been considered חסידים their merit was enough to overcome the Emorites immediately. If the Emorites had refused to vacate the land in order to allow Israel to return there, they could have easily overcome them in battle. Now they had to wait until the Emorites accumulated a further measure of sin before their own moral superiority would suffice to overcome the Emorites in their homeland. Proof of our contention is found in Numbers 14,44 when the Israelites had realised that they had sinned grievously in fearing the Canaanites, and volunteered to fight them. Moses warned them against such an enterprise. They insisted and were defeated. Had they been considered as righteous they certainly would not have been defeated. All of this is an elaboration of what the prophet Chabakuk spoke about. The point is underscored by Moses in Deut. 9.5, where Moses credits Israel's conquest of the land of the Canaanites not to their piety but to the wickedness of the inhabitants of that land at that time. Moses meant that the measure of their sins had become full by that time. Moses told the Israelites at that time that though they were not totally righteous, their merit was sufficient to overcome the complete lack of it in their opponents. He emphasised this lack of righteousness by the Israelites three times! However, the Emorites had absolutely no merits left that they could have used to help them triumph against Israel's various moral shortcomings.